Posted by root
Sun, 06 Jul 2008 11:04:00 GMT
Some of the more rarely used string methods:
str = "the tester"
str["tester"]
str[/\w+\s\w+/]
emulates
capitalize!
str[/(\w+)/] = ($1[0] - 32).chr
RDoc Documentation
Posted in Ruby | Tags string | no comments
Posted by root
Sat, 14 Jun 2008 13:45:00 GMT
class MyString < String
alias + concat
def *(string)
self.split('').inject('') do |s1,c|
s1 + c + string
end
end
def /(string)
(self.split('') - string.split('')).to_s
end
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(":")
Posted in Ruby | Tags overloading, string | no comments