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
13
14
# File 'lib/mightystring/string.rb', line 5

def self.included(base)
  base.class_eval {
    alias_method :at, :[]
    alias_method :bhead, :bisect_head
    alias_method :btail, :bisect_tail

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

Instance Method Details

#bisect_head(offset = 1) ⇒ Object



36
37
38
# File 'lib/mightystring/string.rb', line 36

def bisect_head offset = 1
  [head(offset), tail(offset)]
end

#bisect_tail(offset = 1) ⇒ Object



92
93
94
# File 'lib/mightystring/string.rb', line 92

def bisect_tail offset = 1
  [tail(offset), head(offset)]
end

#del(indexes) ⇒ Object



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

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



28
29
30
# File 'lib/mightystring/string.rb', line 28

def del! indexes
  replace del(indexes)
end

#head(offset = 1) ⇒ Object



32
33
34
# File 'lib/mightystring/string.rb', line 32

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

#index_all(matcher) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/mightystring/string.rb', line 40

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



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

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

#push(str) ⇒ Object



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

def push str
  replace self.+(str)
end

#shiftObject



61
62
63
64
65
# File 'lib/mightystring/string.rb', line 61

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

#sift(chars_to_keep) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mightystring/string.rb', line 67

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



80
81
82
# File 'lib/mightystring/string.rb', line 80

def sort
  chars.sort.join
end

#sort!Object



84
85
86
# File 'lib/mightystring/string.rb', line 84

def sort!
  replace sort
end

#tail(offset = 1) ⇒ Object



88
89
90
# File 'lib/mightystring/string.rb', line 88

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

#unshift(str) ⇒ Object



96
97
98
# File 'lib/mightystring/string.rb', line 96

def unshift str
  replace str.+(self)
end