Class: String

Inherits:
Object show all
Defined in:
lib/hash-utils/string.rb

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

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



819
820
821
# File 'lib/hash-utils/string.rb', line 819

def cut!(range)
    self.replace(self[range])
end

#eighthObject



422
423
424
# File 'lib/hash-utils/string.rb', line 422

def eighth
    self[7].chr
end

#fifthObject



383
384
385
# File 'lib/hash-utils/string.rb', line 383

def fifth
    self[4].chr
end

#firstObject



331
332
333
# File 'lib/hash-utils/string.rb', line 331

def first
    self[0].chr
end

#first_lineObject



484
485
486
# File 'lib/hash-utils/string.rb', line 484

def first_line
    self.first_lines.first
end

#first_lines(count = 1) ⇒ Object



458
459
460
461
462
463
464
465
466
467
# File 'lib/hash-utils/string.rb', line 458

def first_lines(count = 1)
    result = [ ]
    self.each_line do |line|
        count -= 1
        result << line
        break if count == 0
    end
    
    return result
end

#fourthObject



370
371
372
# File 'lib/hash-utils/string.rb', line 370

def fourth
    self[3].chr
end

#gsub_f(from, to = nil, &block) ⇒ Object



283
284
285
286
287
288
289
290
291
# File 'lib/hash-utils/string.rb', line 283

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



307
308
309
310
311
312
313
314
315
316
317
# File 'lib/hash-utils/string.rb', line 307

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



837
838
839
# File 'lib/hash-utils/string.rb', line 837

def interlace(string)
    self.gsub(self.class::INTERLACING, '\1' << string << '\2' << string)
end

#interlace!(string) ⇒ Object



852
853
854
855
# File 'lib/hash-utils/string.rb', line 852

def interlace!(string)
    self.gsub!(self.class::INTERLACING, '\1' << string << '\2' << string)
    return self
end

#lastObject



438
439
440
# File 'lib/hash-utils/string.rb', line 438

def last
    self[-1].chr
end

#last_lineObject



539
540
541
# File 'lib/hash-utils/string.rb', line 539

def last_line
    self.last_lines.last
end

#last_lines(count = 1) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/hash-utils/string.rb', line 504

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

#lcfirstObject



755
756
757
# File 'lib/hash-utils/string.rb', line 755

def lcfirst
    self.dup.lcfirst!
end

#lcfirst!Object



769
770
771
772
# File 'lib/hash-utils/string.rb', line 769

def lcfirst!
    self[0] = self.first.downcase
    return self
end

#map(&block) ⇒ Object



219
220
221
222
223
224
225
226
# File 'lib/hash-utils/string.rb', line 219

def map(&block)
    buffer = " " * self.length
    self.length.times do |i|
        buffer[i] = block.call(self[i]).to_s
    end
            
    return buffer
end

#map!(&block) ⇒ Object



250
251
252
253
254
255
256
# File 'lib/hash-utils/string.rb', line 250

def map!(&block)
    self.length.times do |i|
        self[i] = block.call(self[i]).to_s
    end
    
    return self
end

#numeric?Boolean

Returns:

  • (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



663
664
665
666
667
# File 'lib/hash-utils/string.rb', line 663

def pop(count = 1)
    res = self[(self.length - count)..-1]
    self.replace(self[0..-(count + 1)])
    return res
end

#pop_lineObject



627
628
629
# File 'lib/hash-utils/string.rb', line 627

def pop_line
    self.pop_lines.first
end

#pop_lines(count = 1) ⇒ Object



613
614
615
616
617
618
# File 'lib/hash-utils/string.rb', line 613

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



641
642
643
# File 'lib/hash-utils/string.rb', line 641

def push_lines(*lines)
    self.push("\n" << lines.join("\n"))
end

#secondObject



344
345
346
# File 'lib/hash-utils/string.rb', line 344

def second
    self[1].chr
end

#seventhObject



409
410
411
# File 'lib/hash-utils/string.rb', line 409

def seventh
    self[6].chr
end

#shift(count = 1) ⇒ Object



679
680
681
682
683
# File 'lib/hash-utils/string.rb', line 679

def shift(count = 1)
    res = self[0...count]
    self.replace(self[count..-1])
    return res
end

#shift_lineObject



579
580
581
# File 'lib/hash-utils/string.rb', line 579

def shift_line
    self.shift_lines.first
end

#shift_lines(count = 1) ⇒ Object



563
564
565
566
567
568
# File 'lib/hash-utils/string.rb', line 563

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

#sixthObject



396
397
398
# File 'lib/hash-utils/string.rb', line 396

def sixth
    self[5].chr
end

#string?Boolean

Returns:

  • (Boolean)


783
784
785
# File 'lib/hash-utils/string.rb', line 783

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!



797
798
799
800
801
802
# File 'lib/hash-utils/string.rb', line 797

def swap_with(from)
    intermediate = self.dup
    self.replace(from)
    from.replace(intermediate)
    return self
end

#thirdObject



357
358
359
# File 'lib/hash-utils/string.rb', line 357

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



870
871
872
# File 'lib/hash-utils/string.rb', line 870

def to_boolean(t = "true")
    self == t
end

#ucfirstObject



726
727
728
# File 'lib/hash-utils/string.rb', line 726

def ucfirst
    self.dup.ucfirst!
end

#ucfirst!Object



740
741
742
743
# File 'lib/hash-utils/string.rb', line 740

def ucfirst!
    self[0] = self.first.upcase
    return self
end

#unshift_lines(*lines) ⇒ Object Also known as: unshift_line



593
594
595
# File 'lib/hash-utils/string.rb', line 593

def unshift_lines(*lines)
    self.unshift(lines.join("\n") << "\n")
end