On 6/4/07, Mike Moore <blowmage / gmail.com> wrote:
> 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

Wait a second you might be the first to provide a correct solution, I
just had this crazy idea, so I checked the Quiz. And yes indeed nobody
asked us to print newlines, just the numbers and their substitutes!!!!
Too bad you spoiled it with puts at the end, that adds one newline
that is not part of the spec. But only one, so you are much closer
than most others.
Maybe *nobody* will get the job, as nobody can program up to an easy
spec, putting fancy newlines in there that have not been asked for!

Cheers
Robert
>
> 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
>


-- 
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw