From: Mike Moore Date: 2007-06-05T00:35:14+09:00 Subject: Re: [QUIZ] FizzBuzz (#126) ------=_Part_3705_27284635.1180971314166 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline I didn't spend any time golfing this quiz, so I don't have anything crazy to share. I tried to implement it as I would in an interview - so my intent was clear. My first attempt was procedural. The only thing that may be tricky is that I don't t have any logic specific for printing "FizzBuzz", I build that value when the number is divisible by both 3 and 5. (1..100).each do |i| result = '' result += 'Fizz' if i % 3 == 0 result += 'Buzz' if i % 5 == 0 result = i if result.empty? puts result end That is how I would solve it if I was at a whiteboard. Afterwards I would explain how I would refactor this out to make it reusable, and I would end up with this: class Integer def fizz_buzz result = '' result += 'Fizz' if self % 3 == 0 result += 'Buzz' if self % 5 == 0 result = self if result.empty? result end end (1..100).each { |i| puts i.fizz_buzz } if __FILE__ == $0 ~Mike ------=_Part_3705_27284635.1180971314166--