Ruby Fizz Buzz Solution

My Ruby Fizz Buzz solution - I've been trying to get to know the language better

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'.

FizzBuzz is a nice way 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)
end

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


Recent posts View all

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.