Ruby Fizz Buzz Solution

I decided as part of my wanted to get to know Ruby a bit better I would go back to basics and try out some of the programming challenges that people new to programming are normally asked to try out.

For those that don't know, Fizz Buzz is a children's game where you count upwards from 1, each time the number is divisible by 3 they have to shout Fizz, each time it is divisible by 5 they have to shout Buzz and each time it is divisible by both 3 and 5 they have to shout 'FizzBuzz'.

Obviously this is a trivial task for a seasoned programmer, but it is a nice example to get used to the syntax of a new language.

This was my first attempt - pretty boring and self-explanatory;

for i in (1..20)
    x = ''
    x += "Fizz" if (i % 3 == 0)
    x += "Buzz" if (i % 5 == 0)
    puts(x.empty? ? i : x)

My thinking was that I wanted to keep my puts to a minimum and maintain readability, I think this does just that.

Once I completed the task I started looking around for more creative solutions and stumbled on this code;

Some of the solutions are just awesome - fizzbuzz_sequence7_reverse especially wins it for me and I think if ever I need to show anyone Fizz Buzz I will think up more of the 'exotic' ways of doing it like these guys did.

Recent posts View all

Simple CS


Let's learn about Compilers, what they are and how they work.


Adding an integration to a Slack room

How to add an integration to a Slack room. Part of our Slack mini series.