FizzBuzz in Ruby with Pattern Matching
The addition of Pattern Matching to Ruby since version 2.7 allow for really expressive codeā¦ learning more about Elixir and coming back to Ruby being able to do this feels like cheating:
def fizz_buzz(number)
r = case {r3: number % 3, r5: number % 5, number:}
in { r3: 0, r5: 0} then "FizzBuzz"
in { r3: 0} then "Fizz"
in { r5: 0} then "Buzz"
else number
end
puts r
end
(10..17).each { |n| fizz_buzz n}
#=> Buzz
#=> 11
#=> Fizz
#=> 13
#=> 14
#=> FizzBuzz
#=> 16
#=> 17