Fun with String class methods

Jun 14
2008
class MyString < String
  # not really useful
  alias + concat
 
  # emulates Array '*' method
  def *(string)
    self.split('').inject('') do |s1,c|
      s1 + c + string
    end
  end
 
  # emulates Array '-' method
  def /(string)
    (self.split('') - string.split('')).to_s
  end
 
  # chars' delimiter
  def delimiter(string)
    self.*(string).sub /.$/, ''
  end
 
end
 
a = MyString.new("abcdef")
b = MyString.new("ABCDEF")
 
puts a * ':'
puts a / (b + "a")
puts a.delimiter(":")
 
#a:b:c:d:e:f:
#bcdef
#a:b:c:d:e:f

Calendar

February 2012
M T W T F S S
« Sep    
 12345
6789101112
13141516171819
20212223242526
272829  

Tags