FizzBuzz in Ruby


Jul 02, 2022 | Ruby

If you don't know what FizzBuzz problem is, here's a description:

Write a short program that prints each number from 1 to 100 on a new line.
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.

There are many ways to implement this solution.

(1..100).each do |i|
  fizz = i % 3 == 0
  buzz = i % 5 == 0

  puts case
  when fizz && buzz then "FizzBuzz"
  when fizz then "Fizz"
  when buzz then "Buzz"
  else i
  end
end