Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/bblib/core/util/file.rb,
lib/bblib/cli/color.rb,
lib/bblib/core/util/time.rb,
lib/bblib/core/util/cases.rb,
lib/bblib/core/util/roman.rb,
lib/bblib/core/util/regexp.rb,
lib/bblib/core/util/string.rb,
lib/bblib/core/util/matching.rb,
lib/bblib/core/util/pluralization.rb
Overview
Monkey patches for the String class
Instance Method Summary collapse
- #camel_case(style = :lower) ⇒ Object
- #capital? ⇒ Boolean
- #class_case ⇒ Object
- #composition_similarity(str) ⇒ Object
- #delimited_case(delimiter = '_') ⇒ Object
- #dirname ⇒ Object
- #drop_symbols ⇒ Object
- #drop_symbols! ⇒ Object
- #encap_by?(str) ⇒ Boolean
-
#encap_split(expressions, *delimiters, **opts) ⇒ Object
(also: #esplit)
Split on only delimiters not between specific encapsulators Various characters are special and automatically recognized such as parens which automatically match anything between a begin and end character.
- #encapsulate(char = '"') ⇒ Object
- #extract_floats(convert: true) ⇒ Object
- #extract_integers(convert: true) ⇒ Object
- #extract_numbers(convert: true) ⇒ Object
- #file_name(with_extension = true) ⇒ Object
- #from_roman ⇒ Object
- #levenshtein_distance(str) ⇒ Object
- #levenshtein_similarity(str) ⇒ Object
- #lower? ⇒ Boolean
- #method_case ⇒ Object
- #move_articles(position = :front, capitalize = true) ⇒ Object
- #move_articles!(position = :front, capitalize = true) ⇒ Object
-
#msplit(*delims) ⇒ Object
Multi-split.
- #numeric_similarity(str) ⇒ Object
- #parse_duration(output: :sec, min_interval: :sec) ⇒ Object
- #parse_file_size(*args) ⇒ Object
- #pathify ⇒ Object
- #phrase_similarity(str) ⇒ Object
- #pluralize(num = 2) ⇒ Object
-
#quote_split(*delimiters) ⇒ Object
(also: #qsplit)
Split on delimiters.
- #qwerty_distance(str) ⇒ Object
- #singularize ⇒ Object
- #snake_case ⇒ Object
- #spinal_case ⇒ Object
- #start_case(first_only: false) ⇒ Object
- #title_case(first_only: false) ⇒ Object
-
#to_a ⇒ Object
Simple method to convert a string into an array containing only itself.
- #to_clean_sym ⇒ Object
- #to_color(color_code, opts = {}) ⇒ Object
- #to_file(*args) ⇒ Object
- #to_regex(*options, ignore_invalid: false) ⇒ Object
- #to_roman ⇒ Object
- #train_case ⇒ Object
- #uncapsulate(char = '"', limit: nil) ⇒ Object
- #upper? ⇒ Boolean
Instance Method Details
#camel_case(style = :lower) ⇒ Object
79 80 81 |
# File 'lib/bblib/core/util/cases.rb', line 79 def camel_case(style = :lower) BBLib.camel_case self, style end |
#capital? ⇒ Boolean
257 258 259 |
# File 'lib/bblib/core/util/string.rb', line 257 def capital? chars.first.upper? end |
#class_case ⇒ Object
95 96 97 |
# File 'lib/bblib/core/util/cases.rb', line 95 def class_case BBLib.class_case(self) end |
#composition_similarity(str) ⇒ Object
113 114 115 |
# File 'lib/bblib/core/util/matching.rb', line 113 def composition_similarity(str) BBLib.composition_similarity self, str end |
#delimited_case(delimiter = '_') ⇒ Object
83 84 85 |
# File 'lib/bblib/core/util/cases.rb', line 83 def delimited_case(delimiter = '_') BBLib.delimited_case self, delimiter end |
#dirname ⇒ Object
160 161 162 |
# File 'lib/bblib/core/util/file.rb', line 160 def dirname File.dirname(self) end |
#drop_symbols ⇒ Object
162 163 164 |
# File 'lib/bblib/core/util/string.rb', line 162 def drop_symbols BBLib.drop_symbols self end |
#drop_symbols! ⇒ Object
166 167 168 |
# File 'lib/bblib/core/util/string.rb', line 166 def drop_symbols! replace BBLib.drop_symbols(self) end |
#encap_by?(str) ⇒ Boolean
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/bblib/core/util/string.rb', line 191 def encap_by?(str) case str when '(' start_with?(str) && end_with?(')') when '[' start_with?(str) && end_with?(']') when '{' start_with?(str) && end_with?('}') when '<' start_with?(str) && end_with?('>') else start_with?(str) && end_with?(str) end end |
#encap_split(expressions, *delimiters, **opts) ⇒ Object Also known as: esplit
Split on only delimiters not between specific encapsulators Various characters are special and automatically recognized such as parens which automatically match anything between a begin and end character.
Regex below is no longer used because of how inefficient it is. Comment is left in case it is ever useful again /(?<group>((?:*|g<group>)*)*?),|,(?<=[^()|$])/
148 149 150 |
# File 'lib/bblib/core/util/string.rb', line 148 def encap_split(expressions, *delimiters, **opts) BBLib::Splitter.split(self, *delimiters, **opts.merge(expressions: expressions)) end |
#encapsulate(char = '"') ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/bblib/core/util/string.rb', line 206 def encapsulate(char = '"') back = case char when '(' ')' when '[' ']' when '{' '}' when '<' '>' else char end "#{char}#{self}#{back}" end |
#extract_floats(convert: true) ⇒ Object
174 175 176 |
# File 'lib/bblib/core/util/string.rb', line 174 def extract_floats(convert: true) BBLib.extract_floats self, convert: convert end |
#extract_integers(convert: true) ⇒ Object
170 171 172 |
# File 'lib/bblib/core/util/string.rb', line 170 def extract_integers(convert: true) BBLib.extract_integers self, convert: convert end |
#extract_numbers(convert: true) ⇒ Object
178 179 180 |
# File 'lib/bblib/core/util/string.rb', line 178 def extract_numbers(convert: true) BBLib.extract_numbers self, convert: convert end |
#file_name(with_extension = true) ⇒ Object
156 157 158 |
# File 'lib/bblib/core/util/file.rb', line 156 def file_name(with_extension = true) with_extension ? File.basename(self) : File.basename(self, File.extname(self)) end |
#from_roman ⇒ Object
50 51 52 |
# File 'lib/bblib/core/util/roman.rb', line 50 def from_roman BBLib.from_roman self end |
#levenshtein_distance(str) ⇒ Object
105 106 107 |
# File 'lib/bblib/core/util/matching.rb', line 105 def levenshtein_distance(str) BBLib.levenshtein_distance self, str end |
#levenshtein_similarity(str) ⇒ Object
109 110 111 |
# File 'lib/bblib/core/util/matching.rb', line 109 def levenshtein_similarity(str) BBLib.levenshtein_similarity self, str end |
#lower? ⇒ Boolean
253 254 255 |
# File 'lib/bblib/core/util/string.rb', line 253 def lower? chars.all? { |letter| /[[:lower:]]|\W/.match(letter) } end |
#method_case ⇒ Object
91 92 93 |
# File 'lib/bblib/core/util/cases.rb', line 91 def method_case BBLib.method_case(self) end |
#move_articles(position = :front, capitalize = true) ⇒ Object
154 155 156 |
# File 'lib/bblib/core/util/string.rb', line 154 def move_articles(position = :front, capitalize = true) BBLib.move_articles self, position, capitalize: capitalize end |
#move_articles!(position = :front, capitalize = true) ⇒ Object
158 159 160 |
# File 'lib/bblib/core/util/string.rb', line 158 def move_articles!(position = :front, capitalize = true) replace BBLib.move_articles(self, position, capitalize: capitalize) end |
#msplit(*delims) ⇒ Object
Multi-split. Similar to split, but can be passed an array of delimiters to split on.
125 126 127 128 129 130 131 132 |
# File 'lib/bblib/core/util/string.rb', line 125 def msplit(*delims) ary = [self] return ary if delims.empty? delims.flatten.each do |d| ary = ary.flat_map { |a| a.split d } end ary end |
#numeric_similarity(str) ⇒ Object
121 122 123 |
# File 'lib/bblib/core/util/matching.rb', line 121 def numeric_similarity(str) BBLib.numeric_similarity self, str end |
#parse_duration(output: :sec, min_interval: :sec) ⇒ Object
165 166 167 |
# File 'lib/bblib/core/util/time.rb', line 165 def parse_duration(output: :sec, min_interval: :sec) BBLib.parse_duration self, output: output, min_interval: min_interval end |
#parse_file_size(*args) ⇒ Object
164 165 166 |
# File 'lib/bblib/core/util/file.rb', line 164 def parse_file_size(*args) BBLib.parse_file_size(self, *args) end |
#pathify ⇒ Object
168 169 170 |
# File 'lib/bblib/core/util/file.rb', line 168 def pathify BBLib.pathify(self) end |
#phrase_similarity(str) ⇒ Object
117 118 119 |
# File 'lib/bblib/core/util/matching.rb', line 117 def phrase_similarity(str) BBLib.phrase_similarity self, str end |
#pluralize(num = 2) ⇒ Object
139 140 141 |
# File 'lib/bblib/core/util/pluralization.rb', line 139 def pluralize(num = 2) BBLib.pluralize(self, num) end |
#quote_split(*delimiters) ⇒ Object Also known as: qsplit
Split on delimiters
135 136 137 |
# File 'lib/bblib/core/util/string.rb', line 135 def quote_split(*delimiters) encap_split(%w{" '}, *delimiters) end |
#qwerty_distance(str) ⇒ Object
125 126 127 |
# File 'lib/bblib/core/util/matching.rb', line 125 def qwerty_distance(str) BBLib.qwerty_distance self, str end |
#singularize ⇒ Object
143 144 145 |
# File 'lib/bblib/core/util/pluralization.rb', line 143 def singularize BBLib.singularize(self) end |
#snake_case ⇒ Object
87 88 89 |
# File 'lib/bblib/core/util/cases.rb', line 87 def snake_case BBLib.snake_case self end |
#spinal_case ⇒ Object
99 100 101 |
# File 'lib/bblib/core/util/cases.rb', line 99 def spinal_case BBLib.spinal_case self end |
#start_case(first_only: false) ⇒ Object
75 76 77 |
# File 'lib/bblib/core/util/cases.rb', line 75 def start_case(first_only: false) BBLib.start_case self, first_only: first_only end |
#title_case(first_only: false) ⇒ Object
71 72 73 |
# File 'lib/bblib/core/util/cases.rb', line 71 def title_case(first_only: false) BBLib.title_case self, first_only: first_only end |
#to_a ⇒ Object
Simple method to convert a string into an array containing only itself
187 188 189 |
# File 'lib/bblib/core/util/string.rb', line 187 def to_a [self] end |
#to_clean_sym ⇒ Object
182 183 184 |
# File 'lib/bblib/core/util/string.rb', line 182 def to_clean_sym snake_case.to_sym end |
#to_color(color_code, opts = {}) ⇒ Object
48 49 50 |
# File 'lib/bblib/cli/color.rb', line 48 def to_color(color_code, opts = {}) BBLib::Console.colorize(self, color_code, **opts) end |
#to_file(*args) ⇒ Object
152 153 154 |
# File 'lib/bblib/core/util/file.rb', line 152 def to_file(*args) BBLib.string_to_file(self, *args) end |
#to_regex(*options, ignore_invalid: false) ⇒ Object
42 43 44 |
# File 'lib/bblib/core/util/regexp.rb', line 42 def to_regex(*, ignore_invalid: false) Regexp.from_s(self, *, ignore_invalid: ignore_invalid) end |
#to_roman ⇒ Object
54 55 56 |
# File 'lib/bblib/core/util/roman.rb', line 54 def to_roman BBLib.string_to_roman self end |
#train_case ⇒ Object
103 104 105 |
# File 'lib/bblib/core/util/cases.rb', line 103 def train_case BBLib.train_case self end |
#uncapsulate(char = '"', limit: nil) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/bblib/core/util/string.rb', line 222 def uncapsulate(char = '"', limit: nil) back = case char when '(' ')' when '[' ']' when '{' '}' when '<' '>' else char end temp = dup count = 0 while temp.start_with?(char) && temp != char && (limit.nil? || count < limit) temp = temp[(char.size)..-1] count += 1 end count = 0 while temp.end_with?(back) && temp != char && (limit.nil? || count < limit) temp = temp[0..-(char.size + 1)] count += 1 end temp end |
#upper? ⇒ Boolean
249 250 251 |
# File 'lib/bblib/core/util/string.rb', line 249 def upper? chars.all? { |letter| /[[:upper:]]|\W/.match(letter) } end |