In common with many other languages, Ruby has a syntactic shortcut:
a=a+2
may be written as
a+=2
.
The second form is converted internally to the first. This means that
operators that you have defined as methods in your own classes work as
you'd expect.
class Bowdlerize
|
def initialize(aString)
|
@value = aString.gsub(/[aeiou]/, '*')
|
end
|
def +(other)
|
Bowdlerize.new(self.to_s + other.to_s)
|
end
|
def to_s
|
@value
|
end
|
end
|
|
a = Bowdlerize.new("damn ")
|
� |
d*mn
|
a += "shame"
|
� |
d*mn sh*m*
|