My solution, which is probably awkward in its use of method mangling:
class Fixnum
@@old_to_s = 1.method(:to_s).unbind
def to_s
s = ((self % 3 == 0 ? "Fizz" : "") + (self % 5 == 0 ? "Buzz" : ""))
s.empty? ? @@old_to_s.bind(self).call : s
end
end
(1..100).each { |x| p x }
I wouldn't be surprised at all to find that there's a much cleaner way
to accomplish the override. The curious reader will note that this could
be shortened somewhat if we had something like that "it" feature some
were discussing.
-s