Module: MightyString::String

Defined in:
lib/mightystring/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/mightystring/string.rb', line 5

def self.included(base)
  base.class_eval {
    alias_method :at, :[]

    extend Forwardable
    def_delegators :chars, :first, :last, :values_at
  }
end

Instance Method Details

#del(indexes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mightystring/string.rb', line 14

def del indexes
  case indexes
  when String then split(indexes).join
  else
    each_char.with_index.
      reduce([]) {|arr,(c,i)|
        arr << c unless Array(indexes).include?(i)
        arr
    }.join
  end
end

#del!(indexes) ⇒ Object



26
27
28
# File 'lib/mightystring/string.rb', line 26

def del! indexes
  replace del(indexes)
end

#head(offset = 1) ⇒ Object



30
31
32
# File 'lib/mightystring/string.rb', line 30

def head offset = 1
  self[0...offset]
end

#index_all(matcher) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/mightystring/string.rb', line 34

def index_all matcher
  arr_indexes = []
  srch_index = rindex(matcher)
  while srch_index do
    tmpStr = self[0..srch_index-1]
    arr_indexes << srch_index 
    srch_index = srch_index.zero? ? nil : tmpStr.rindex(matcher)
  end
  arr_indexes.reverse
end

#popObject



45
46
47
48
49
# File 'lib/mightystring/string.rb', line 45

def pop
  chr = self[-1]
  replace chop
  chr 
end

#push(str) ⇒ Object



51
52
53
# File 'lib/mightystring/string.rb', line 51

def push str
  replace self.+(str)
end

#shiftObject



55
56
57
58
59
# File 'lib/mightystring/string.rb', line 55

def shift
  chr = self[0]
  replace self[1..-1]
  chr
end

#sift(chars_to_keep) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mightystring/string.rb', line 61

def sift chars_to_keep
  chars_to_keep = case chars_to_keep
                  when String then chars_to_keep.chars
                  when Array  then chars_to_keep
                  when Range  then chars_to_keep.to_a
                  else
                    raise TypeError, "Invalid parameter"
                  end
  chars.keep_if {|chr|
    chars_to_keep.include? chr
  }.join
end

#sortObject



74
75
76
# File 'lib/mightystring/string.rb', line 74

def sort
  chars.sort.join
end

#sort!Object



78
79
80
# File 'lib/mightystring/string.rb', line 78

def sort!
  replace sort
end

#tail(offset = 1) ⇒ Object



82
83
84
# File 'lib/mightystring/string.rb', line 82

def tail offset = 1
  self[offset..-1]
end

#unshift(str) ⇒ Object



86
87
88
# File 'lib/mightystring/string.rb', line 86

def unshift str
  replace str.+(self)
end