Class: String
Instance Method Summary collapse
- #acronym ⇒ Object
- #acronym! ⇒ Object
- #any?(*keys) ⇒ Boolean
- #ascii_only(alt = '') ⇒ Object
- #ascii_only!(alt = '') ⇒ Object
- #at(position) ⇒ Object
- #camelize(first_letter = :upper) ⇒ Object (also: #camelcase)
- #camelize!(first_letter = :upper) ⇒ Object (also: #camelcase!)
- #capitalized? ⇒ Boolean
- #classify ⇒ Object
- #classify! ⇒ Object
- #constantize ⇒ Object
- #dasherize ⇒ Object
- #dasherize! ⇒ Object
- #deconstantize ⇒ Object
- #deconstantize! ⇒ Object
- #dedupe(pattern) ⇒ Object
- #dedupe!(pattern) ⇒ Object
- #demodulize ⇒ Object
- #demodulize! ⇒ Object
- #domain ⇒ Object
- #downcase? ⇒ Boolean
- #each_word(&block) ⇒ Object
- #ellipsize(ellipsize_at, options = {}) ⇒ Object
- #encode_only(encoding, alt = '') ⇒ Object
- #encode_only!(encoding, alt = '') ⇒ Object
- #first(limit = 1) ⇒ Object
- #format(*args) ⇒ Object
- #from(position) ⇒ Object
- #headerize ⇒ Object
- #headerize! ⇒ Object
- #humanize(options = {}) ⇒ Object
- #humanize!(capitalize: true) ⇒ Object
- #indent(amount, seperator = ' ') ⇒ Object
- #indent!(amount, seperator = ' ') ⇒ Object
- #index_all(pattern) ⇒ Object
- #labelize(options = {}) ⇒ Object (also: #labelcase)
- #labelize!(capitalize: true) ⇒ Object (also: #labelcase!)
- #last(limit = 1) ⇒ Object
- #lchomp(match) ⇒ Object
- #lchomp!(match) ⇒ Object
- #methodize ⇒ Object
- #methodize! ⇒ Object
- #mixedcase? ⇒ Boolean
- #modulize ⇒ Object
- #modulize! ⇒ Object
- #non_possessive ⇒ Object
- #non_possessive! ⇒ Object
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #parameterize(separator: '-') ⇒ Object
- #parameterize!(separator: '-') ⇒ Object
- #pathize ⇒ Object
- #pathize! ⇒ Object
- #pollute(delimiter = '^--^--^') ⇒ Object
- #pollute!(delimiter = '^--^--^') ⇒ Object
- #pop ⇒ Object
- #possessive ⇒ Object
- #possessive! ⇒ Object
- #possessive? ⇒ Boolean
- #push(string) ⇒ Object
-
#quote(type = :double, amount = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
-
#quote!(type = :double, amount = nil) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
- #remove(*patterns) ⇒ Object
- #remove!(*patterns) ⇒ Object
- #remove_tags ⇒ Object
- #remove_tags! ⇒ Object
- #rotate(amount = 1) ⇒ Object
- #rotate!(amount = 1) ⇒ 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 (also: #snakecase)
- #underscore! ⇒ Object (also: #snakecase!)
- #unpollute(delimiter = '^--^--^') ⇒ Object
- #unpollute!(delimiter = '^--^--^') ⇒ Object
- #unquote ⇒ Object
- #unquote! ⇒ Object
- #unshift(*patterns) ⇒ Object
- #unshift!(*patterns) ⇒ Object
- #upcase? ⇒ Boolean
- #variablize ⇒ Object
- #variablize! ⇒ Object
- #words ⇒ Object
- #words_without_punctuation ⇒ Object
Instance Method Details
#acronym ⇒ Object
36 37 38 |
# File 'lib/lite/ruby/string.rb', line 36 def acronym dup.acronym! end |
#acronym! ⇒ Object
40 41 42 |
# File 'lib/lite/ruby/string.rb', line 40 def acronym! gsub!(/(([a-zA-Z0-9])([a-zA-Z0-9])*)./, '\\2') || self end |
#any?(*keys) ⇒ Boolean
44 45 46 |
# File 'lib/lite/ruby/string.rb', line 44 def any?(*keys) keys.any? { |key| include?(key) } end |
#ascii_only(alt = '') ⇒ Object
48 49 50 |
# File 'lib/lite/ruby/string.rb', line 48 def ascii_only(alt = '') dup.ascii_only!(alt) end |
#ascii_only!(alt = '') ⇒ Object
52 53 54 |
# File 'lib/lite/ruby/string.rb', line 52 def ascii_only!(alt = '') encode_only!('ASCII', alt) end |
#at(position) ⇒ Object
56 57 58 |
# File 'lib/lite/ruby/string.rb', line 56 def at(position) self[position] end |
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
60 61 62 63 64 65 |
# File 'lib/lite/ruby/string.rb', line 60 def camelize(first_letter = :upper) case first_letter when :upper, true then modulize.gsub(/(\A|\s)([a-z])/) { $1 + $2.upcase } when :lower, false then modulize.gsub(/(\A|\s)([A-Z])/) { $1 + $2.downcase } end || modulize end |
#camelize!(first_letter = :upper) ⇒ Object Also known as: camelcase!
69 70 71 |
# File 'lib/lite/ruby/string.rb', line 69 def camelize!(first_letter = :upper) replace(camelize(first_letter)) end |
#capitalized? ⇒ Boolean
75 76 77 |
# File 'lib/lite/ruby/string.rb', line 75 def capitalized? capitalize == self end |
#classify ⇒ Object
79 80 81 |
# File 'lib/lite/ruby/string.rb', line 79 def classify dup.classify! end |
#classify! ⇒ Object
83 84 85 86 |
# File 'lib/lite/ruby/string.rb', line 83 def classify! sub!(/.*\./, '') camelize! end |
#constantize ⇒ Object
88 89 90 |
# File 'lib/lite/ruby/string.rb', line 88 def constantize Object.const_get(camelize) end |
#dasherize ⇒ Object
92 93 94 |
# File 'lib/lite/ruby/string.rb', line 92 def dasherize underscore.tr('_', '-') end |
#dasherize! ⇒ Object
96 97 98 |
# File 'lib/lite/ruby/string.rb', line 96 def dasherize! replace(dasherize) end |
#deconstantize ⇒ Object
100 101 102 |
# File 'lib/lite/ruby/string.rb', line 100 def deconstantize [0, rindex('::') || 0] end |
#deconstantize! ⇒ Object
104 105 106 |
# File 'lib/lite/ruby/string.rb', line 104 def deconstantize! replace(deconstantize) end |
#dedupe(pattern) ⇒ Object
108 109 110 |
# File 'lib/lite/ruby/string.rb', line 108 def dedupe(pattern) dup.dedupe!(pattern) end |
#dedupe!(pattern) ⇒ Object
112 113 114 115 |
# File 'lib/lite/ruby/string.rb', line 112 def dedupe!(pattern) pattern.each_char { |char| gsub!(/#{Regexp.escape(char)}{2,}/, char) } self end |
#demodulize ⇒ Object
117 118 119 |
# File 'lib/lite/ruby/string.rb', line 117 def demodulize gsub(/^.*::/, '') end |
#demodulize! ⇒ Object
121 122 123 |
# File 'lib/lite/ruby/string.rb', line 121 def demodulize! replace(demodulize) end |
#domain ⇒ Object
125 126 127 128 129 |
# File 'lib/lite/ruby/string.rb', line 125 def domain return self unless self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)} Regexp.last_match(1) end |
#downcase? ⇒ Boolean
131 132 133 |
# File 'lib/lite/ruby/string.rb', line 131 def downcase? downcase == self end |
#each_word(&block) ⇒ Object
135 136 137 |
# File 'lib/lite/ruby/string.rb', line 135 def each_word(&block) words.each(&block) end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/lite/ruby/string.rb', line 154 def ellipsize(ellipsize_at, = {}) return self if length <= ellipsize_at separator = [:separator] || '...' offset = [:offset] || 4 "#{self[0, offset]}#{separator}#{self[-offset, offset]}" end |
#encode_only(encoding, alt = '') ⇒ Object
139 140 141 |
# File 'lib/lite/ruby/string.rb', line 139 def encode_only(encoding, alt = '') dup.encode_only!(encoding, alt) end |
#encode_only!(encoding, alt = '') ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/lite/ruby/string.rb', line 143 def encode_only!(encoding, alt = '') = { invalid: :replace, undef: :replace, replace: alt, UNIVERSAL_NEWLINE_DECORATOR: true } encode!(Encoding.find(encoding), ) end |
#first(limit = 1) ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/lite/ruby/string.rb', line 163 def first(limit = 1) if limit.zero? '' elsif limit >= length self else to(limit - 1) end end |
#format(*args) ⇒ Object
173 174 175 |
# File 'lib/lite/ruby/string.rb', line 173 def format(*args) super(self, *args.flatten) end |
#from(position) ⇒ Object
177 178 179 |
# File 'lib/lite/ruby/string.rb', line 177 def from(position) self[position..-1] end |
#headerize ⇒ Object
181 182 183 |
# File 'lib/lite/ruby/string.rb', line 181 def headerize squish.each_word(&:capitalize!).join(' ') end |
#headerize! ⇒ Object
185 186 187 |
# File 'lib/lite/ruby/string.rb', line 185 def headerize! replace(headerize) end |
#humanize(options = {}) ⇒ Object
189 190 191 |
# File 'lib/lite/ruby/string.rb', line 189 def humanize( = {}) dup.humanize!() end |
#humanize!(capitalize: true) ⇒ Object
193 194 195 196 197 198 199 200 |
# File 'lib/lite/ruby/string.rb', line 193 def humanize!(capitalize: true) underscore! gsub!(/_id\z/, '') tr!('_', ' ') squish! gsub!(/([a-z\d]*)/i, &:downcase) gsub!(/\A\w/) { |str| capitalize ? str.upcase : str } || self end |
#indent(amount, seperator = ' ') ⇒ Object
202 203 204 |
# File 'lib/lite/ruby/string.rb', line 202 def indent(amount, seperator = ' ') dup.indent!(amount, seperator) end |
#indent!(amount, seperator = ' ') ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/lite/ruby/string.rb', line 206 def indent!(amount, seperator = ' ') if amount >= 0 gsub!(/^/, seperator * amount) else gsub!(/^#{Regexp.escape(seperator)}{0,#{-amount}}/, '') end end |
#index_all(pattern) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/lite/ruby/string.rb', line 214 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
228 229 230 |
# File 'lib/lite/ruby/string.rb', line 228 def labelize( = {}) dup.labelize!() end |
#labelize!(capitalize: true) ⇒ Object Also known as: labelcase!
234 235 236 237 238 239 240 241 |
# File 'lib/lite/ruby/string.rb', line 234 def labelize!(capitalize: true) underscore! tr!('_', ' ') squish! gsub!(/([a-z\d]*)/i, &:downcase) gsub!(/\A\w/) { |str| capitalize ? str.upcase : str } gsub!(/ id\z/, ' ID') || self end |
#last(limit = 1) ⇒ Object
245 246 247 248 249 250 251 252 253 |
# File 'lib/lite/ruby/string.rb', line 245 def last(limit = 1) if limit.zero? '' elsif limit >= length self else from(-limit) end end |
#lchomp(match) ⇒ Object
255 256 257 |
# File 'lib/lite/ruby/string.rb', line 255 def lchomp(match) dup.lchomp!(match) end |
#lchomp!(match) ⇒ Object
259 260 261 262 263 264 |
# File 'lib/lite/ruby/string.rb', line 259 def lchomp!(match) return self unless index(match) self[0...match.size] = '' self end |
#methodize ⇒ Object
266 267 268 |
# File 'lib/lite/ruby/string.rb', line 266 def methodize dup.methodize! end |
#methodize! ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/lite/ruby/string.rb', line 270 def methodize! gsub!(/([A-Z]+)([A-Z])/, '\1_\2') gsub!(/([a-z])([A-Z])/, '\1_\2') gsub!('/', '__') gsub!('::', '__') downcase! || self end |
#mixedcase? ⇒ Boolean
289 290 291 |
# File 'lib/lite/ruby/string.rb', line 289 def mixedcase? !upcase? && !downcase? end |
#modulize ⇒ Object
278 279 280 |
# File 'lib/lite/ruby/string.rb', line 278 def modulize dup.modulize! end |
#modulize! ⇒ Object
282 283 284 285 286 287 |
# File 'lib/lite/ruby/string.rb', line 282 def modulize! gsub!(/__(.?)/) { "::#{$1.upcase}" } gsub!(%r{/(.?)}) { "::#{$1.upcase}" } gsub!(/(?:_+|-+)([a-z])/) { $1.upcase } gsub!(/(\A|\s)([a-z])/) { $1 + $2.upcase } || self end |
#non_possessive ⇒ Object
337 338 339 |
# File 'lib/lite/ruby/string.rb', line 337 def non_possessive dup.non_possessive! end |
#non_possessive! ⇒ Object
341 342 343 344 345 |
# File 'lib/lite/ruby/string.rb', line 341 def non_possessive! return self unless possessive? chomp!("'s") || chomp!("'") || self end |
#ordinal ⇒ Object
293 294 295 |
# File 'lib/lite/ruby/string.rb', line 293 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
297 298 299 |
# File 'lib/lite/ruby/string.rb', line 297 def ordinalize to_i.ordinalize end |
#parameterize(separator: '-') ⇒ Object
301 302 303 |
# File 'lib/lite/ruby/string.rb', line 301 def parameterize(separator: '-') dup.parameterize!(separator: separator) end |
#parameterize!(separator: '-') ⇒ Object
305 306 307 308 309 |
# File 'lib/lite/ruby/string.rb', line 305 def parameterize!(separator: '-') underscore! gsub!(/\s+/, separator) downcase! || self end |
#pathize ⇒ Object
311 312 313 |
# File 'lib/lite/ruby/string.rb', line 311 def pathize dup.pathize! end |
#pathize! ⇒ Object
315 316 317 318 319 320 321 322 323 |
# File 'lib/lite/ruby/string.rb', line 315 def pathize! gsub!(/([A-Z]+)([A-Z])/, '\1_\2') gsub!(/([a-z])([A-Z])/, '\1_\2') gsub!('__', '/') gsub!('::', '/') gsub!(/\s+/, '') gsub!(/[?%*:|"<>.]+/, '') downcase! || self end |
#pollute(delimiter = '^--^--^') ⇒ Object
325 326 327 |
# File 'lib/lite/ruby/string.rb', line 325 def pollute(delimiter = '^--^--^') chars.map { |chr| "#{chr}#{delimiter}" }.join end |
#pollute!(delimiter = '^--^--^') ⇒ Object
329 330 331 |
# File 'lib/lite/ruby/string.rb', line 329 def pollute!(delimiter = '^--^--^') replace(pollute(delimiter)) end |
#pop ⇒ Object
333 334 335 |
# File 'lib/lite/ruby/string.rb', line 333 def pop self[-1] end |
#possessive ⇒ Object
347 348 349 350 351 352 |
# File 'lib/lite/ruby/string.rb', line 347 def possessive return self if possessive? possession = end_with?('s') ? "'" : "'s" "#{self}#{possession}" end |
#possessive! ⇒ Object
354 355 356 |
# File 'lib/lite/ruby/string.rb', line 354 def possessive! replace(possessive) end |
#possessive? ⇒ Boolean
358 359 360 |
# File 'lib/lite/ruby/string.rb', line 358 def possessive? %w['s s'].any? { |pos| end_with?(pos) } end |
#push(string) ⇒ Object
362 363 364 |
# File 'lib/lite/ruby/string.rb', line 362 def push(string) replace(concat(string)) end |
#quote(type = :double, amount = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/lite/ruby/string.rb', line 367 def quote(type = :double, amount = nil) if type.is_a?(Integer) tmp = amount amount = type type = tmp || :mixed else amount ||= 1 end case type.to_s when "'", 'single', 's', '1' f = "'" * amount b = f when '"', 'double', 'd', '2' f = '"' * amount b = f when '`', 'back', 'backtick', 'b', '-1' f = '`' * amount b = f when "`'", 'bracket', 'sb' f = '`' * amount b = "'" * amount when "'\"", 'mixed', 'm', 'Integer' c = (amount.to_f / 2).to_i f = '"' * c b = f if amount.odd? f = "'" + f b += "'" end else raise ArgumentError, "Invalid quote type: #{type.inspect}" end "#{f}#{self}#{b}" end |
#quote!(type = :double, amount = nil) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
406 407 408 |
# File 'lib/lite/ruby/string.rb', line 406 def quote!(type = :double, amount = nil) replace(quote(type, amount)) end |
#remove(*patterns) ⇒ Object
410 411 412 |
# File 'lib/lite/ruby/string.rb', line 410 def remove(*patterns) dup.remove!(*patterns) end |
#remove!(*patterns) ⇒ Object
414 415 416 417 418 |
# File 'lib/lite/ruby/string.rb', line 414 def remove!(*patterns) patterns.each_with_object(self) do |pat, str| pat.is_a?(Range) ? str.slice!(pat) : str.gsub!(pat, '') end end |
#remove_tags ⇒ Object
420 421 422 |
# File 'lib/lite/ruby/string.rb', line 420 def dup. end |
#remove_tags! ⇒ Object
424 425 426 |
# File 'lib/lite/ruby/string.rb', line 424 def gsub!(%r{<\/?[^>]*>}, '') || self end |
#rotate(amount = 1) ⇒ Object
428 429 430 |
# File 'lib/lite/ruby/string.rb', line 428 def rotate(amount = 1) dup.rotate!(amount) end |
#rotate!(amount = 1) ⇒ Object
432 433 434 435 |
# File 'lib/lite/ruby/string.rb', line 432 def rotate!(amount = 1) amount += size if amount.negative? slice!(amount, size - amount) + slice!(0, amount) end |
#sample(separator = ' ') ⇒ Object
437 438 439 |
# File 'lib/lite/ruby/string.rb', line 437 def sample(separator = ' ') split(separator).sample end |
#sample!(separator = ' ') ⇒ Object
441 442 443 |
# File 'lib/lite/ruby/string.rb', line 441 def sample!(separator = ' ') replace(sample(separator)) end |
#shift(*patterns) ⇒ Object
445 446 447 |
# File 'lib/lite/ruby/string.rb', line 445 def shift(*patterns) dup.shift!(*patterns) end |
#shift!(*patterns) ⇒ Object
449 450 451 452 453 |
# File 'lib/lite/ruby/string.rb', line 449 def shift!(*patterns) return self[0] if patterns.empty? patterns.each_with_object(self) { |pat, str| str.sub!(pat, '') } end |
#shuffle(separator = '') ⇒ Object
455 456 457 |
# File 'lib/lite/ruby/string.rb', line 455 def shuffle(separator = '') split(separator).shuffle.join end |
#shuffle!(separator = '') ⇒ Object
459 460 461 |
# File 'lib/lite/ruby/string.rb', line 459 def shuffle!(separator = '') replace(shuffle(separator)) end |
#sift(keep) ⇒ Object
463 464 465 466 467 468 469 470 471 472 |
# File 'lib/lite/ruby/string.rb', line 463 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
474 475 476 |
# File 'lib/lite/ruby/string.rb', line 474 def sift!(keep) replace(sift(keep)) end |
#slugify ⇒ Object
478 479 480 |
# File 'lib/lite/ruby/string.rb', line 478 def slugify dup.slugify! end |
#slugify! ⇒ Object
482 483 484 485 486 487 488 |
# File 'lib/lite/ruby/string.rb', line 482 def slugify! gsub!(/[^\x00-\x7F]+/, '') gsub!(/[^\w_ \-]+/i, '') gsub!(/[ \-]+/i, '-') gsub!(/^\-|\-$/i, '') downcase! || self end |
#sort ⇒ Object
499 500 501 |
# File 'lib/lite/ruby/string.rb', line 499 def sort chars.sort.join end |
#sort! ⇒ Object
503 504 505 |
# File 'lib/lite/ruby/string.rb', line 503 def sort! replace(sort) end |
#squish ⇒ Object
490 491 492 |
# File 'lib/lite/ruby/string.rb', line 490 def squish dup.squish! end |
#squish! ⇒ Object
494 495 496 497 |
# File 'lib/lite/ruby/string.rb', line 494 def squish! strip! gsub!(/\s+/, ' ') || self end |
#titleize ⇒ Object Also known as: titlecase
507 508 509 |
# File 'lib/lite/ruby/string.rb', line 507 def titleize dup.titleize! end |
#titleize! ⇒ Object Also known as: titlecase!
513 514 515 516 517 |
# File 'lib/lite/ruby/string.rb', line 513 def titleize! underscore! humanize! gsub!(/\b(?<!['’`])[a-z]/) { $&.capitalize } || self end |
#to(position) ⇒ Object
521 522 523 |
# File 'lib/lite/ruby/string.rb', line 521 def to(position) self[0..position] end |
#transliterize ⇒ Object
525 526 527 |
# File 'lib/lite/ruby/string.rb', line 525 def transliterize dup.transliterize! end |
#transliterize! ⇒ Object
529 530 531 |
# File 'lib/lite/ruby/string.rb', line 529 def transliterize! TRANSLITERATIONS.each_with_object(self) { |(k, v), str| str.gsub!(k, v) } end |
#truncate(truncate_at, options = {}) ⇒ Object
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File 'lib/lite/ruby/string.rb', line 533 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
549 550 551 552 553 554 555 |
# File 'lib/lite/ruby/string.rb', line 549 def truncate_words(words_count, = {}) sep = [:separator] || /\s+/ sep = Regexp.escape(sep.to_s) unless sep.is_a(Regexp) return self unless /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m.match?(self) "#{::Regexp.last_match(1)}#{[:omissio] || '...'}" end |
#underscore ⇒ Object Also known as: snakecase
557 558 559 |
# File 'lib/lite/ruby/string.rb', line 557 def underscore dup.underscore! end |
#underscore! ⇒ Object Also known as: snakecase!
563 564 565 566 567 568 569 570 |
# File 'lib/lite/ruby/string.rb', line 563 def underscore! camelize! gsub!(/::/, '/') gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') gsub!(/([a-z\d])([A-Z])/, '\1_\2') tr!('-', '_') downcase! || self end |
#unpollute(delimiter = '^--^--^') ⇒ Object
574 575 576 |
# File 'lib/lite/ruby/string.rb', line 574 def unpollute(delimiter = '^--^--^') dup.unpollute!(delimiter) end |
#unpollute!(delimiter = '^--^--^') ⇒ Object
578 579 580 |
# File 'lib/lite/ruby/string.rb', line 578 def unpollute!(delimiter = '^--^--^') gsub!(delimiter, '') || self end |
#unquote ⇒ Object
595 596 597 |
# File 'lib/lite/ruby/string.rb', line 595 def unquote dup.unquote! end |
#unquote! ⇒ Object
599 600 601 602 603 604 605 606 607 |
# File 'lib/lite/ruby/string.rb', line 599 def unquote! [0, -1].each do |i| case self[i, 1] when "'", '"', '`' then self[i] = '' end end self end |
#unshift(*patterns) ⇒ Object
586 587 588 589 |
# File 'lib/lite/ruby/string.rb', line 586 def unshift(*patterns) patterns.each_with_object('') { |pat, str| str << pat } .concat(self) end |
#unshift!(*patterns) ⇒ Object
591 592 593 |
# File 'lib/lite/ruby/string.rb', line 591 def unshift!(*patterns) replace(unshift(*patterns)) end |
#upcase? ⇒ Boolean
582 583 584 |
# File 'lib/lite/ruby/string.rb', line 582 def upcase? upcase == self end |
#variablize ⇒ Object
622 623 624 |
# File 'lib/lite/ruby/string.rb', line 622 def variablize "@#{gsub(/\W/, '_')}" end |
#variablize! ⇒ Object
626 627 628 |
# File 'lib/lite/ruby/string.rb', line 626 def variablize! replace(variablize) end |
#words ⇒ Object
609 610 611 |
# File 'lib/lite/ruby/string.rb', line 609 def words split(/\s+/) end |
#words_without_punctuation ⇒ Object
613 614 615 616 617 618 619 620 |
# File 'lib/lite/ruby/string.rb', line 613 def words_without_punctuation str = dup str.gsub!(%r{[.?¿¡…!,::;—"。?!、‘“”„«»〈〉《》,/\[\]]}, ' ') str.gsub!('- ', ' ') str.squeeze!(' ') str.strip! str.words end |