Solving the Fizzbuzz Puzzle with Ruby

So back in February Jeff Atwood over at was talking about a puzzle to give prospective new-hires when interviewing them entitled “Fizzbuzz”. You can read more about it here.

So with a little thought I decided to solve the fizzbuzz puzzle using Rudy and an Array.

count = 0
100.times do
  count += 1
  fb = []
  fb << "Fizz" if (count % 3) == 0
  fb << "Buzz" if (count % 5) == 0
  fb << count if (count % 3) != 0 and (count % 5) != 0
  puts (fb.join "")

For those of you that are curious as to why I used a local variable of count rather then the index identifier, is because the article by Jeff Atwood requests a loop from 1-100 and Ruby starts looping at 0.


2 thoughts on “Solving the Fizzbuzz Puzzle with Ruby”

  1. Well, the post was from a year and a half ago, so at the time I wasn’t really familiar with upto, but you’re right… and this point if I was to write this it would look something like:

    1.upto(100) do |x|
    fb = []
    fb << “Fizz” if (x % 3) == 0
    fb << “Buzz” if (x % 5) == 0
    fb << x if (x % 3) != 0 and (x % 5) != 0
    puts (fb.join “”)

Leave a Reply to S2K Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s