module MightyString::String

Public Class Methods

included(base) click to toggle source
# 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

Public Instance Methods

bisect_head(offset = 1) click to toggle source
# File lib/mightystring/string.rb, line 36
def bisect_head offset = 1
  [head(offset), tail(offset)]
end
bisect_tail(offset = 1) click to toggle source
# File lib/mightystring/string.rb, line 92
def bisect_tail offset = 1
  [tail(offset), head(offset)]
end
del(indexes) click to toggle source
# 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) click to toggle source
# File lib/mightystring/string.rb, line 28
def del! indexes
  replace del(indexes)
end
head(offset = 1) click to toggle source
# File lib/mightystring/string.rb, line 32
def head offset = 1
  self[0...offset]
end
index_all(matcher) click to toggle source
# 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
pop() click to toggle source
# File lib/mightystring/string.rb, line 51
def pop
  chr = self[-1]
  replace chop
  chr 
end
push(str) click to toggle source
# File lib/mightystring/string.rb, line 57
def push str
  replace self.+(str)
end
shift() click to toggle source
# File lib/mightystring/string.rb, line 61
def shift
  chr = self[0]
  replace self[1..-1]
  chr
end
sift(chars_to_keep) click to toggle source
# 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
sort() click to toggle source
# File lib/mightystring/string.rb, line 80
def sort
  chars.sort.join
end
sort!() click to toggle source
# File lib/mightystring/string.rb, line 84
def sort!
  replace sort
end
tail(offset = 1) click to toggle source
# File lib/mightystring/string.rb, line 88
def tail offset = 1
  self[offset..-1]
end
unshift(str) click to toggle source
# File lib/mightystring/string.rb, line 96
def unshift str
  replace str.+(self)
end