Class: String
Instance Method Summary collapse
- #any?(*keys) ⇒ Boolean
- #at(position) ⇒ Object
- #camelize(first_letter = :upper) ⇒ Object (also: #camelcase)
- #camelize!(first_letter = :upper) ⇒ Object (also: #camelcase!)
- #classify ⇒ Object
- #classify! ⇒ Object
- #constantize ⇒ Object
- #dasherize ⇒ Object
- #dasherize! ⇒ Object
- #deconstantize ⇒ Object
- #deconstantize! ⇒ Object
- #demodulize ⇒ Object
- #demodulize! ⇒ Object
- #domain ⇒ Object
- #downcase? ⇒ Boolean
- #ellipsize(ellipsize_at, options = {}) ⇒ Object
- #first(limit = 1) ⇒ Object
- #format(*args) ⇒ Object
- #from(position) ⇒ Object
- #headerize ⇒ Object
- #headerize! ⇒ Object
- #humanize(options = {}) ⇒ Object
- #humanize!(options = {}) ⇒ Object
- #indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #index_all(pattern) ⇒ Object
- #labelize(options = {}) ⇒ Object (also: #labelcase)
- #labelize!(options = {}) ⇒ Object (also: #labelcase!)
- #last(limit = 1) ⇒ Object
- #mixedcase? ⇒ Boolean
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #parameterize(separator: '-') ⇒ Object
- #parameterize!(separator: '-') ⇒ Object
- #pollute(delimiter = '^--^--^') ⇒ Object
- #pollute!(delimiter = '^--^--^') ⇒ Object
- #pop ⇒ Object
- #push(string) ⇒ Object
- #remove(*patterns) ⇒ Object
- #remove!(*patterns) ⇒ Object
- #remove_tags ⇒ Object
- #remove_tags! ⇒ Object
- #sample(separator = ' ') ⇒ Object
- #sample!(separator = ' ') ⇒ Object
- #shift(*patterns) ⇒ Object
- #shift!(*patterns) ⇒ Object
- #shuffle(separator = '') ⇒ Object
- #shuffle!(separator = '') ⇒ Object
- #sift(keep) ⇒ Object
- #sift!(keep) ⇒ Object
- #slugify ⇒ Object
- #slugify! ⇒ Object
- #sort ⇒ Object
- #sort! ⇒ Object
- #squish ⇒ Object
- #squish! ⇒ Object
- #titleize ⇒ Object (also: #titlecase)
- #titleize! ⇒ Object (also: #titlecase!)
- #to(position) ⇒ Object
- #transliterize ⇒ Object
- #transliterize! ⇒ Object
- #truncate(truncate_at, options = {}) ⇒ Object
- #truncate_words(words_count, options = {}) ⇒ Object
- #underscore ⇒ Object
- #underscore! ⇒ Object
- #unpollute(delimiter = '^--^--^') ⇒ Object
- #unpollute!(delimiter = '^--^--^') ⇒ Object
- #unshift(*patterns) ⇒ Object
- #unshift!(*patterns) ⇒ Object
- #upcase? ⇒ Boolean
Instance Method Details
#any?(*keys) ⇒ Boolean
35 36 37 |
# File 'lib/lite/ruby/string.rb', line 35 def any?(*keys) keys.any? { |key| include?(key) } end |
#at(position) ⇒ Object
39 40 41 |
# File 'lib/lite/ruby/string.rb', line 39 def at(position) self[position] end |
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
43 44 45 46 47 48 49 50 |
# File 'lib/lite/ruby/string.rb', line 43 def camelize(first_letter = :upper) if first_letter.to_sym != :lower regex_last = Regexp.last_match(1).upcase to_s.gsub(%r{\/(.?)}) { "::#{regex_last}" }.gsub(%r{^/(?:^|_)(.)}) { regex_last } else "#{to_s.first.chr.downcase}#{camelize(self)[1..-1]}" end end |
#camelize!(first_letter = :upper) ⇒ Object Also known as: camelcase!
54 55 56 |
# File 'lib/lite/ruby/string.rb', line 54 def camelize!(first_letter = :upper) replace(camelize(first_letter)) end |
#classify ⇒ Object
60 61 62 |
# File 'lib/lite/ruby/string.rb', line 60 def classify to_s.sub(/.*\./, '').camelize end |
#classify! ⇒ Object
64 65 66 |
# File 'lib/lite/ruby/string.rb', line 64 def classify! replace(classify) end |
#constantize ⇒ Object
68 69 70 |
# File 'lib/lite/ruby/string.rb', line 68 def constantize Object.const_get(self) end |
#dasherize ⇒ Object
72 73 74 |
# File 'lib/lite/ruby/string.rb', line 72 def dasherize tr(/_/, '-') end |
#dasherize! ⇒ Object
76 77 78 |
# File 'lib/lite/ruby/string.rb', line 76 def dasherize! replace(dasherize) end |
#deconstantize ⇒ Object
80 81 82 |
# File 'lib/lite/ruby/string.rb', line 80 def deconstantize to_s[0, rindex('::') || 0] end |
#deconstantize! ⇒ Object
84 85 86 |
# File 'lib/lite/ruby/string.rb', line 84 def deconstantize! replace(deconstantize) end |
#demodulize ⇒ Object
88 89 90 |
# File 'lib/lite/ruby/string.rb', line 88 def demodulize to_s.gsub(/^.*::/, '') end |
#demodulize! ⇒ Object
92 93 94 |
# File 'lib/lite/ruby/string.rb', line 92 def demodulize! replace(demodulize) end |
#domain ⇒ Object
96 97 98 99 100 |
# File 'lib/lite/ruby/string.rb', line 96 def domain return self unless self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)} Regexp.last_match(1) end |
#downcase? ⇒ Boolean
102 103 104 |
# File 'lib/lite/ruby/string.rb', line 102 def downcase? downcase == self end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/lite/ruby/string.rb', line 106 def ellipsize(ellipsize_at, = {}) return self if length <= ellipsize_at separator = [:separator] || '...' offset = [:offset] || 4 "#{self[0, offset]}#{separator}#{self[-offset, offset]}" end |
#first(limit = 1) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/lite/ruby/string.rb', line 115 def first(limit = 1) if limit.zero? '' elsif limit >= length self else to(limit - 1) end end |
#format(*args) ⇒ Object
125 126 127 |
# File 'lib/lite/ruby/string.rb', line 125 def format(*args) super(self, *args.flatten) end |
#from(position) ⇒ Object
129 130 131 |
# File 'lib/lite/ruby/string.rb', line 129 def from(position) self[position..-1] end |
#headerize ⇒ Object
133 134 135 |
# File 'lib/lite/ruby/string.rb', line 133 def headerize squish.split(' ').map(&:capitalize).join(' ') end |
#headerize! ⇒ Object
137 138 139 |
# File 'lib/lite/ruby/string.rb', line 137 def headerize! replace(headerize) end |
#humanize(options = {}) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/lite/ruby/string.rb', line 141 def humanize( = {}) capitalize = [:capitalize] || true underscore.gsub(/_id\z/, '') .tr('_', ' ') .squish .gsub(/([a-z\d]*)/i, &:downcase) .gsub(/\A\w/) { |str| capitalize ? str.upcase : str } end |
#humanize!(options = {}) ⇒ Object
151 152 153 |
# File 'lib/lite/ruby/string.rb', line 151 def humanize!( = {}) replace(humanize()) end |
#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/lite/ruby/string.rb', line 155 def indent(amount, indent_string = nil, indent_empty_lines = false) indent_string = indent_string || self[/^[ \t]/] || ' ' substitutes = indent_empty_lines ? /^/ : /^(?!$)/ gsub(substitutes, indent_string * amount) end |
#indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
162 163 164 |
# File 'lib/lite/ruby/string.rb', line 162 def indent!(amount, indent_string = nil, indent_empty_lines = false) replace(indent(amount, indent_string, indent_empty_lines)) end |
#index_all(pattern) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/lite/ruby/string.rb', line 166 def index_all(pattern) pattern = pattern.to_s if pattern.is_a?(Numeric) arr_indexes = [] srch_index = rindex(pattern) while srch_index temp_string = self[0..(srch_index - 1)] arr_indexes << srch_index srch_index = srch_index.zero? ? nil : temp_string.rindex(pattern) end arr_indexes.reverse end |
#labelize(options = {}) ⇒ Object Also known as: labelcase
180 181 182 183 184 185 186 187 188 |
# File 'lib/lite/ruby/string.rb', line 180 def labelize( = {}) capitalize = [:capitalize] || true underscore.tr('_', ' ') .squish .gsub(/([a-z\d]*)/i, &:downcase) .gsub(/\A\w/) { |str| capitalize ? str.upcase : str } .gsub(/ id\z/, ' ID') end |
#labelize!(options = {}) ⇒ Object Also known as: labelcase!
192 193 194 |
# File 'lib/lite/ruby/string.rb', line 192 def labelize!( = {}) replace(labelize()) end |
#last(limit = 1) ⇒ Object
198 199 200 201 202 203 204 205 206 |
# File 'lib/lite/ruby/string.rb', line 198 def last(limit = 1) if limit.zero? '' elsif limit >= length self else from(-limit) end end |
#mixedcase? ⇒ Boolean
208 209 210 |
# File 'lib/lite/ruby/string.rb', line 208 def mixedcase? !upcase? && !downcase? end |
#ordinal ⇒ Object
212 213 214 |
# File 'lib/lite/ruby/string.rb', line 212 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
216 217 218 |
# File 'lib/lite/ruby/string.rb', line 216 def ordinalize to_i.ordinalize end |
#parameterize(separator: '-') ⇒ Object
220 221 222 |
# File 'lib/lite/ruby/string.rb', line 220 def parameterize(separator: '-') underscore.gsub(/\s+/, separator).downcase end |
#parameterize!(separator: '-') ⇒ Object
224 225 226 |
# File 'lib/lite/ruby/string.rb', line 224 def parameterize!(separator: '-') replace(parameterize(separator: separator)) end |
#pollute(delimiter = '^--^--^') ⇒ Object
228 229 230 |
# File 'lib/lite/ruby/string.rb', line 228 def pollute(delimiter = '^--^--^') split('').map { |chr| "#{chr}#{delimiter}" }.join end |
#pollute!(delimiter = '^--^--^') ⇒ Object
232 233 234 |
# File 'lib/lite/ruby/string.rb', line 232 def pollute!(delimiter = '^--^--^') replace(pollute(delimiter)) end |
#pop ⇒ Object
236 237 238 |
# File 'lib/lite/ruby/string.rb', line 236 def pop self[-1] end |
#push(string) ⇒ Object
240 241 242 |
# File 'lib/lite/ruby/string.rb', line 240 def push(string) replace(concat(string)) end |
#remove(*patterns) ⇒ Object
244 245 246 247 248 |
# File 'lib/lite/ruby/string.rb', line 244 def remove(*patterns) patterns.each_with_object(dup) do |pat, str| pat.is_a?(Range) ? str.slice!(pat) : str.gsub!(pat, '') end end |
#remove!(*patterns) ⇒ Object
250 251 252 |
# File 'lib/lite/ruby/string.rb', line 250 def remove!(*patterns) replace(remove(*patterns)) end |
#remove_tags ⇒ Object
254 255 256 |
# File 'lib/lite/ruby/string.rb', line 254 def gsub(%r{<\/?[^>]*>}, '') end |
#remove_tags! ⇒ Object
258 259 260 |
# File 'lib/lite/ruby/string.rb', line 258 def replace() end |
#sample(separator = ' ') ⇒ Object
262 263 264 |
# File 'lib/lite/ruby/string.rb', line 262 def sample(separator = ' ') split(separator).sample end |
#sample!(separator = ' ') ⇒ Object
266 267 268 |
# File 'lib/lite/ruby/string.rb', line 266 def sample!(separator = ' ') replace(sample(separator)) end |
#shift(*patterns) ⇒ Object
270 271 272 273 274 |
# File 'lib/lite/ruby/string.rb', line 270 def shift(*patterns) return self[0] if patterns.empty? patterns.each_with_object(dup) { |pat, str| str.sub!(pat, '') } end |
#shift!(*patterns) ⇒ Object
276 277 278 |
# File 'lib/lite/ruby/string.rb', line 276 def shift!(*patterns) replace(shift(*patterns)) end |
#shuffle(separator = '') ⇒ Object
280 281 282 |
# File 'lib/lite/ruby/string.rb', line 280 def shuffle(separator = '') split(separator).shuffle.join end |
#shuffle!(separator = '') ⇒ Object
284 285 286 |
# File 'lib/lite/ruby/string.rb', line 284 def shuffle!(separator = '') replace(shuffle(separator)) end |
#sift(keep) ⇒ Object
288 289 290 291 292 293 294 295 296 297 |
# File 'lib/lite/ruby/string.rb', line 288 def sift(keep) keep = case keep when String then keep.chars when Array then keep.map(&:to_s) when Range then keep.to_a.map(&:to_s) else raise TypeError, "Invalid parameter #{keep.inspect}" end chars.keep_if { |chr| keep.include?(chr) }.join end |
#sift!(keep) ⇒ Object
299 300 301 |
# File 'lib/lite/ruby/string.rb', line 299 def sift!(keep) replace(sift(keep)) end |
#slugify ⇒ Object
303 304 305 306 307 308 309 |
# File 'lib/lite/ruby/string.rb', line 303 def slugify to_s.gsub(/[^\x00-\x7F]+/, '') .gsub(/[^\w_ \-]+/i, '') .gsub(/[ \-]+/i, '-') .gsub(/^\-|\-$/i, '') .downcase end |
#slugify! ⇒ Object
311 312 313 |
# File 'lib/lite/ruby/string.rb', line 311 def slugify! replace(slugify) end |
#sort ⇒ Object
323 324 325 |
# File 'lib/lite/ruby/string.rb', line 323 def sort chars.sort.join end |
#sort! ⇒ Object
327 328 329 |
# File 'lib/lite/ruby/string.rb', line 327 def sort! replace(sort) end |
#squish ⇒ Object
315 316 317 |
# File 'lib/lite/ruby/string.rb', line 315 def squish strip.gsub(/\s+/, ' ') end |
#squish! ⇒ Object
319 320 321 |
# File 'lib/lite/ruby/string.rb', line 319 def squish! replace(squish) end |
#titleize ⇒ Object Also known as: titlecase
331 332 333 |
# File 'lib/lite/ruby/string.rb', line 331 def titleize underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize } end |
#titleize! ⇒ Object Also known as: titlecase!
337 338 339 |
# File 'lib/lite/ruby/string.rb', line 337 def titleize! replace(titleize) end |
#to(position) ⇒ Object
343 344 345 |
# File 'lib/lite/ruby/string.rb', line 343 def to(position) self[0..position] end |
#transliterize ⇒ Object
347 348 349 |
# File 'lib/lite/ruby/string.rb', line 347 def transliterize dup.transliterize! end |
#transliterize! ⇒ Object
351 352 353 |
# File 'lib/lite/ruby/string.rb', line 351 def transliterize! TRANSLITERATIONS.each_with_object(self) { |(k, v), str| str.gsub!(k, v) } end |
#truncate(truncate_at, options = {}) ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/lite/ruby/string.rb', line 355 def truncate(truncate_at, = {}) return dup unless length > truncate_at seperator = [:separator] omission = [:omission] || '...' size_with_room_for_omission = truncate_at - omission.length stop = if seperator rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission else size_with_room_for_omission end "#{self[0, stop]}#{omission}" end |
#truncate_words(words_count, options = {}) ⇒ Object
371 372 373 374 375 376 377 |
# File 'lib/lite/ruby/string.rb', line 371 def truncate_words(words_count, = {}) sep = [:separator] || /\s+/ sep = Regexp.escape(sep.to_s) unless sep.is_a(Regexp) return self unless self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m "#{::Regexp.last_match(1)}#{[:omissio] || '...'}" end |
#underscore ⇒ Object
379 380 381 382 383 384 385 |
# File 'lib/lite/ruby/string.rb', line 379 def underscore to_s.gsub(/::/, '/') .gsub(/([A-Z\d]+)([A-Z][a-z])/, "\1_\2") .gsub(/([a-z\d])([A-Z])/, "\1_\2") .tr('-', '_') .downcase end |
#underscore! ⇒ Object
387 388 389 |
# File 'lib/lite/ruby/string.rb', line 387 def underscore! replace(underscore) end |
#unpollute(delimiter = '^--^--^') ⇒ Object
391 392 393 |
# File 'lib/lite/ruby/string.rb', line 391 def unpollute(delimiter = '^--^--^') gsub(delimiter, '') end |
#unpollute!(delimiter = '^--^--^') ⇒ Object
395 396 397 |
# File 'lib/lite/ruby/string.rb', line 395 def unpollute!(delimiter = '^--^--^') replace(unpollute(delimiter)) end |
#unshift(*patterns) ⇒ Object
403 404 405 406 |
# File 'lib/lite/ruby/string.rb', line 403 def unshift(*patterns) patterns.each_with_object('') { |pat, str| str << pat } .concat(self) end |
#unshift!(*patterns) ⇒ Object
408 409 410 |
# File 'lib/lite/ruby/string.rb', line 408 def unshift!(*patterns) replace(unshift(*patterns)) end |
#upcase? ⇒ Boolean
399 400 401 |
# File 'lib/lite/ruby/string.rb', line 399 def upcase? upcase == self end |