Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/file/bbfile.rb,
lib/time/bbtime.rb,
lib/string/cases.rb,
lib/string/roman.rb,
lib/string/regexp.rb,
lib/string/bbstring.rb,
lib/string/matching.rb,
lib/string/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(encapsulator, *delimiters) ⇒ 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_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
77 78 79 |
# File 'lib/string/cases.rb', line 77 def camel_case(style = :lower) BBLib.camel_case self, style end |
#capital? ⇒ Boolean
263 264 265 |
# File 'lib/string/bbstring.rb', line 263 def capital? chars.first.upper? end |
#class_case ⇒ Object
93 94 95 |
# File 'lib/string/cases.rb', line 93 def class_case BBLib.class_case(self) end |
#composition_similarity(str) ⇒ Object
113 114 115 |
# File 'lib/string/matching.rb', line 113 def composition_similarity(str) BBLib.composition_similarity self, str end |
#delimited_case(delimiter = '_') ⇒ Object
81 82 83 |
# File 'lib/string/cases.rb', line 81 def delimited_case(delimiter = '_') BBLib.delimited_case self, delimiter end |
#dirname ⇒ Object
126 127 128 |
# File 'lib/file/bbfile.rb', line 126 def dirname File.dirname(self) end |
#drop_symbols ⇒ Object
168 169 170 |
# File 'lib/string/bbstring.rb', line 168 def drop_symbols BBLib.drop_symbols self end |
#drop_symbols! ⇒ Object
172 173 174 |
# File 'lib/string/bbstring.rb', line 172 def drop_symbols! replace BBLib.drop_symbols(self) end |
#encap_by?(str) ⇒ Boolean
197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/string/bbstring.rb', line 197 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(encapsulator, *delimiters) ⇒ 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.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/string/bbstring.rb', line 141 def encap_split(encapsulator, *delimiters) pattern = case encapsulator when '(' '\\(\\)' when '[' '\\[\\]' when '{' '\\{\\}' when '<' '\\<\\>' else encapsulator end patterns = delimiters.map { |d| /#{Regexp.escape(d)}(?=(?:[^#{pattern}]|[#{pattern}][^#{pattern}]*[#{pattern}])*$)/} msplit(*patterns) end |
#encapsulate(char = '"') ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/string/bbstring.rb', line 212 def encapsulate(char = '"') back = case char when '(' ')' when '[' ']' when '{' '}' when '<' '>' else char end "#{char}#{self}#{back}" end |
#extract_floats(convert: true) ⇒ Object
180 181 182 |
# File 'lib/string/bbstring.rb', line 180 def extract_floats(convert: true) BBLib.extract_floats self, convert: convert end |
#extract_integers(convert: true) ⇒ Object
176 177 178 |
# File 'lib/string/bbstring.rb', line 176 def extract_integers(convert: true) BBLib.extract_integers self, convert: convert end |
#extract_numbers(convert: true) ⇒ Object
184 185 186 |
# File 'lib/string/bbstring.rb', line 184 def extract_numbers(convert: true) BBLib.extract_numbers self, convert: convert end |
#file_name(with_extension = true) ⇒ Object
122 123 124 |
# File 'lib/file/bbfile.rb', line 122 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/string/roman.rb', line 50 def from_roman BBLib.from_roman self end |
#levenshtein_distance(str) ⇒ Object
105 106 107 |
# File 'lib/string/matching.rb', line 105 def levenshtein_distance(str) BBLib.levenshtein_distance self, str end |
#levenshtein_similarity(str) ⇒ Object
109 110 111 |
# File 'lib/string/matching.rb', line 109 def levenshtein_similarity(str) BBLib.levenshtein_similarity self, str end |
#lower? ⇒ Boolean
259 260 261 |
# File 'lib/string/bbstring.rb', line 259 def lower? chars.all? { |letter| /[[:lower:]]|\W/.match(letter) } end |
#method_case ⇒ Object
89 90 91 |
# File 'lib/string/cases.rb', line 89 def method_case BBLib.method_case(self) end |
#move_articles(position = :front, capitalize = true) ⇒ Object
160 161 162 |
# File 'lib/string/bbstring.rb', line 160 def move_articles(position = :front, capitalize = true) BBLib.move_articles self, position, capitalize: capitalize end |
#move_articles!(position = :front, capitalize = true) ⇒ Object
164 165 166 |
# File 'lib/string/bbstring.rb', line 164 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.
122 123 124 125 126 127 128 129 |
# File 'lib/string/bbstring.rb', line 122 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/string/matching.rb', line 121 def numeric_similarity(str) BBLib.numeric_similarity self, str end |
#parse_duration(output: :sec, min_interval: :sec) ⇒ Object
169 170 171 |
# File 'lib/time/bbtime.rb', line 169 def parse_duration(output: :sec, min_interval: :sec) BBLib.parse_duration self, output: output, min_interval: min_interval end |
#parse_file_size(*args) ⇒ Object
130 131 132 |
# File 'lib/file/bbfile.rb', line 130 def parse_file_size(*args) BBLib.parse_file_size(self, *args) end |
#pathify ⇒ Object
134 135 136 |
# File 'lib/file/bbfile.rb', line 134 def pathify BBLib.pathify(self) end |
#phrase_similarity(str) ⇒ Object
117 118 119 |
# File 'lib/string/matching.rb', line 117 def phrase_similarity(str) BBLib.phrase_similarity self, str end |
#pluralize(num = 2) ⇒ Object
139 140 141 |
# File 'lib/string/pluralization.rb', line 139 def pluralize(num = 2) BBLib.pluralize(self, num) end |
#quote_split(*delimiters) ⇒ Object Also known as: qsplit
Split on delimiters
132 133 134 |
# File 'lib/string/bbstring.rb', line 132 def quote_split(*delimiters) encap_split('"\'', *delimiters) end |
#qwerty_distance(str) ⇒ Object
125 126 127 |
# File 'lib/string/matching.rb', line 125 def qwerty_distance(str) BBLib.qwerty_distance self, str end |
#singularize ⇒ Object
143 144 145 |
# File 'lib/string/pluralization.rb', line 143 def singularize BBLib.singularize(self) end |
#snake_case ⇒ Object
85 86 87 |
# File 'lib/string/cases.rb', line 85 def snake_case BBLib.snake_case self end |
#spinal_case ⇒ Object
97 98 99 |
# File 'lib/string/cases.rb', line 97 def spinal_case BBLib.spinal_case self end |
#start_case(first_only: false) ⇒ Object
73 74 75 |
# File 'lib/string/cases.rb', line 73 def start_case(first_only: false) BBLib.start_case self, first_only: first_only end |
#title_case(first_only: false) ⇒ Object
69 70 71 |
# File 'lib/string/cases.rb', line 69 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
193 194 195 |
# File 'lib/string/bbstring.rb', line 193 def to_a [self] end |
#to_clean_sym ⇒ Object
188 189 190 |
# File 'lib/string/bbstring.rb', line 188 def to_clean_sym snake_case.to_sym end |
#to_file(*args) ⇒ Object
118 119 120 |
# File 'lib/file/bbfile.rb', line 118 def to_file(*args) BBLib.string_to_file(self, *args) end |
#to_regex(*options, ignore_invalid: false) ⇒ Object
42 43 44 |
# File 'lib/string/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/string/roman.rb', line 54 def to_roman BBLib.string_to_roman self end |
#train_case ⇒ Object
101 102 103 |
# File 'lib/string/cases.rb', line 101 def train_case BBLib.train_case self end |
#uncapsulate(char = '"', limit: nil) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/string/bbstring.rb', line 228 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
255 256 257 |
# File 'lib/string/bbstring.rb', line 255 def upper? chars.all? { |letter| /[[:upper:]]|\W/.match(letter) } end |