Class: String
Overview
String extension.
Constant Summary collapse
- NUMERIC =
/^\s*-?\d+(?:\.\d+)?\s*$/
- INTERLACING =
/(.)([^$])/
- RANDOM_ALPHA_LOWER =
"abcdefghijklmnopqrstuvwxyz"
- RANDOM_ALPHA_UPPER =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- RANDOM_NUMBERS =
"0123456789"
- RANDOM_SYMBOLS =
"!\/#?.,_-+*@%&{}[]()=<>|~'$"
- RANDOM_WHITESPACE =
" "
- RANDOM_FULL =
self::RANDOM_ALPHA_LOWER + self::RANDOM_ALPHA_UPPER + self::RANDOM_NUMBERS + self::RANDOM_SYMBOLS + self::RANDOM_WHITESPACE
Class Method Summary collapse
Instance Method Summary collapse
- #cut!(range) ⇒ Object
- #eighth ⇒ Object
- #fifth ⇒ Object
- #first ⇒ Object
- #first_line ⇒ Object
- #first_lines(count = 1) ⇒ Object
- #fourth ⇒ Object
- #gsub_f(from, to = nil, &block) ⇒ Object
- #gsub_f!(from, to = nil, &block) ⇒ Object
- #interlace(string) ⇒ Object
- #interlace!(string) ⇒ Object
- #last ⇒ Object
- #last_line ⇒ Object
- #last_lines(count = 1) ⇒ Object
- #lcfirst ⇒ Object
- #lcfirst! ⇒ Object
- #map(&block) ⇒ Object
- #map!(&block) ⇒ Object
- #numeric? ⇒ Boolean
- #pop(count = 1) ⇒ Object
- #pop_line ⇒ Object
- #pop_lines(count = 1) ⇒ Object
- #push_lines(*lines) ⇒ Object (also: #push_line)
- #second ⇒ Object
- #seventh ⇒ Object
- #shift(count = 1) ⇒ Object
- #shift_line ⇒ Object
- #shift_lines(count = 1) ⇒ Object
- #sixth ⇒ Object
- #string? ⇒ Boolean
- #strtr(defs, mode = nil, &block) ⇒ Object
- #strtr!(defs, mode = nil, &block) ⇒ Object
- #swap_with(from) ⇒ Object (also: #swap_with!)
- #third ⇒ Object
- #to_a(glue = "") ⇒ Object
- #to_boolean(t = "true") ⇒ Object
- #ucfirst ⇒ Object
- #ucfirst! ⇒ Object
- #unshift_lines(*lines) ⇒ Object (also: #unshift_line)
Class Method Details
.random(length, characters = self::RANDOM_FULL) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/hash-utils/string.rb', line 97 def self.random(length, characters = self::RANDOM_FULL) result = "" max = characters.length length.times do result << characters[Kernel.rand(max)].chr end return result end |
Instance Method Details
#cut!(range) ⇒ Object
798 799 800 |
# File 'lib/hash-utils/string.rb', line 798 def cut!(range) self.replace(self[range]) end |
#eighth ⇒ Object
401 402 403 |
# File 'lib/hash-utils/string.rb', line 401 def eighth self[7].chr end |
#fifth ⇒ Object
362 363 364 |
# File 'lib/hash-utils/string.rb', line 362 def fifth self[4].chr end |
#first ⇒ Object
310 311 312 |
# File 'lib/hash-utils/string.rb', line 310 def first self.chr end |
#first_line ⇒ Object
463 464 465 |
# File 'lib/hash-utils/string.rb', line 463 def first_line self.first_lines.first end |
#first_lines(count = 1) ⇒ Object
437 438 439 440 441 442 443 444 445 446 |
# File 'lib/hash-utils/string.rb', line 437 def first_lines(count = 1) result = [ ] self.each_line do |line| count -= 1 result << line break if count == 0 end return result end |
#fourth ⇒ Object
349 350 351 |
# File 'lib/hash-utils/string.rb', line 349 def fourth self[3].chr end |
#gsub_f(from, to = nil, &block) ⇒ Object
262 263 264 265 266 267 268 269 270 |
# File 'lib/hash-utils/string.rb', line 262 def gsub_f(from, to = nil, &block) __prepare_gsub_f(from, to, block) do |callback| if to.nil? self.gsub(from, &callback) else self.gsub(from, to) end end end |
#gsub_f!(from, to = nil, &block) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/hash-utils/string.rb', line 286 def gsub_f!(from, to = nil, &block) __prepare_gsub_f(from, to, block) do |callback| if to.nil? self.gsub!(from, &callback) else self.gsub!(from, to) end end return self end |
#interlace(string) ⇒ Object
816 817 818 |
# File 'lib/hash-utils/string.rb', line 816 def interlace(string) self.gsub(self.class::INTERLACING, '\1' << string << '\2' << string) end |
#interlace!(string) ⇒ Object
831 832 833 834 |
# File 'lib/hash-utils/string.rb', line 831 def interlace!(string) self.gsub!(self.class::INTERLACING, '\1' << string << '\2' << string) return self end |
#last ⇒ Object
417 418 419 |
# File 'lib/hash-utils/string.rb', line 417 def last self[-1].chr end |
#last_line ⇒ Object
518 519 520 |
# File 'lib/hash-utils/string.rb', line 518 def last_line self.last_lines.last end |
#last_lines(count = 1) ⇒ Object
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/hash-utils/string.rb', line 483 def last_lines(count = 1) buffer = "" result = [ ] (self.length - 1).downto(0) do |i| chr = self[i] if chr.ord == 10 count -= 1 result << buffer.reverse! buffer = "" break if count == 0 end buffer << chr.chr end if count != 0 result << buffer.reverse! end return result.reverse! end |
#lcfirst ⇒ Object
734 735 736 |
# File 'lib/hash-utils/string.rb', line 734 def lcfirst self.dup.lcfirst! end |
#lcfirst! ⇒ Object
748 749 750 751 |
# File 'lib/hash-utils/string.rb', line 748 def lcfirst! self[0] = self.first.downcase return self end |
#map(&block) ⇒ Object
218 219 220 221 222 223 224 225 |
# File 'lib/hash-utils/string.rb', line 218 def map(&block) buffer = " " * self.length self.length.times do |i| buffer[i] = block.call(self[i]) end return buffer end |
#map!(&block) ⇒ Object
238 239 240 241 242 243 244 |
# File 'lib/hash-utils/string.rb', line 238 def map!(&block) self.length.times do |i| self[i] = block.call(self[i]) end return self end |
#numeric? ⇒ Boolean
116 117 118 119 120 121 122 |
# File 'lib/hash-utils/string.rb', line 116 def numeric? if self.match(self.class::NUMERIC) true else false end end |
#pop(count = 1) ⇒ Object
642 643 644 645 646 |
# File 'lib/hash-utils/string.rb', line 642 def pop(count = 1) res = self[(self.length - count)..-1] self.replace(self[0..-(count + 1)]) return res end |
#pop_line ⇒ Object
606 607 608 |
# File 'lib/hash-utils/string.rb', line 606 def pop_line self.pop_lines.first end |
#pop_lines(count = 1) ⇒ Object
592 593 594 595 596 597 |
# File 'lib/hash-utils/string.rb', line 592 def pop_lines(count = 1) lines = self.last_lines(count) length = lines.inject(0) { |sum, i| sum + i.length } self.replace(self[0..(length - 1)]) return lines end |
#push_lines(*lines) ⇒ Object Also known as: push_line
620 621 622 |
# File 'lib/hash-utils/string.rb', line 620 def push_lines(*lines) self.push("\n" << lines.join("\n")) end |
#second ⇒ Object
323 324 325 |
# File 'lib/hash-utils/string.rb', line 323 def second self[1].chr end |
#seventh ⇒ Object
388 389 390 |
# File 'lib/hash-utils/string.rb', line 388 def seventh self[6].chr end |
#shift(count = 1) ⇒ Object
658 659 660 661 662 |
# File 'lib/hash-utils/string.rb', line 658 def shift(count = 1) res = self[0...count] self.replace(self[count..-1]) return res end |
#shift_line ⇒ Object
558 559 560 |
# File 'lib/hash-utils/string.rb', line 558 def shift_line self.shift_lines.first end |
#shift_lines(count = 1) ⇒ Object
542 543 544 545 546 547 |
# File 'lib/hash-utils/string.rb', line 542 def shift_lines(count = 1) lines = self.first_lines(count) length = lines.reduce(0) { |sum, i| sum + i.length } self.replace(self[length..-1]) return lines end |
#sixth ⇒ Object
375 376 377 |
# File 'lib/hash-utils/string.rb', line 375 def sixth self[5].chr end |
#string? ⇒ Boolean
762 763 764 |
# File 'lib/hash-utils/string.rb', line 762 def string? true end |
#strtr(defs, mode = nil, &block) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/hash-utils/string.rb', line 154 def strtr(defs, mode = nil, &block) if block.nil? block = Proc::new { |s| s } end defs, matcher = __prepare_strtr(defs, mode) self.gsub(matcher) { |s| defs[block.call(s)] } end |
#strtr!(defs, mode = nil, &block) ⇒ Object
177 178 179 180 181 182 183 184 |
# File 'lib/hash-utils/string.rb', line 177 def strtr!(defs, mode = nil, &block) if block.nil? block = Proc::new { |s| s } end defs, matcher = __prepare_strtr(defs, mode) self.gsub!(matcher) { |s| defs[block.call(s)] } end |
#swap_with(from) ⇒ Object Also known as: swap_with!
776 777 778 779 780 781 |
# File 'lib/hash-utils/string.rb', line 776 def swap_with(from) intermediate = self.dup self.replace(from) from.replace(intermediate) return self end |
#third ⇒ Object
336 337 338 |
# File 'lib/hash-utils/string.rb', line 336 def third self[2].chr end |
#to_a(glue = "") ⇒ Object
200 201 202 |
# File 'lib/hash-utils/string.rb', line 200 def to_a(glue = "") self.split(glue) end |
#to_boolean(t = "true") ⇒ Object
849 850 851 |
# File 'lib/hash-utils/string.rb', line 849 def to_boolean(t = "true") self == t end |
#ucfirst ⇒ Object
705 706 707 |
# File 'lib/hash-utils/string.rb', line 705 def ucfirst self.dup.ucfirst! end |
#ucfirst! ⇒ Object
719 720 721 722 |
# File 'lib/hash-utils/string.rb', line 719 def ucfirst! self[0] = self.first.upcase return self end |
#unshift_lines(*lines) ⇒ Object Also known as: unshift_line
572 573 574 |
# File 'lib/hash-utils/string.rb', line 572 def unshift_lines(*lines) self.unshift(lines.join("\n") << "\n") end |