# Re: FizzBuzz (#126)

**URL:** <https://www.ruby-forum.com/t/re-fizzbuzz-126/105824>\
**Category:** Ruby\
**Created:** [June 3, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/re-fizzbuzz-126/105824 "2007-06-03T13:23:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 3, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/re-fizzbuzz-126/105824/1 "2007-06-03T13:23:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 3, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/re-fizzbuzz-126/105824/2 "2007-06-03T17:44:22Z")

</div>

On 6/3/07, Peter S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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… 🙂

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 3, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/re-fizzbuzz-126/105824/3 "2007-06-03T20:18:43Z")

</div>

In message  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), “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
