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
On 6/3/07, Peter S. [email protected] wrote:
(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
My solution (see other thread) uses a lot of those same techniques.
My method was overriding #inspect instead of #to_s, so you don’t need
to do anything crazy. Or you could just use an alias…
In message
[email protected], “Chris
Carter” writes
:
My solution (see other thread) uses a lot of those same techniques.
My method was overriding #inspect instead of #to_s, so you don’t need
to do anything crazy. Or you could just use an alias…
It’s this last one that I didn’t spot, not being quite used to the
method-wrangling stuff.
-s