I am feeling that following solution
class Fixnum
def +(x)
self + x + 1
end
end
should not work, since +
will be called recursively.
I am feeling that following solution
class Fixnum
def +(x)
self + x + 1
end
end
should not work, since +
will be called recursively.
Using alias
to store the original +
like this works:
class Fixnum
alias old_plus +
def +(x)
old_plus(x).succ
end
end
Another way is to prepend
a module:
module PlusOne
def +(x)
super.succ
end
end
Fixnum.prepend(PlusOne)
1 + 1 #=> 3