{ MANOYLO }

FizzBuzz in Ruby

How to implement FizzBuzz solution in Ruby

Ruby

Posted on Mar 26, 2020

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.

Here's one of them:
(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