Created
January 28, 2024 08:56
-
-
Save t0yohei/04ae5784908bad3daf46fee60e69ad54 to your computer and use it in GitHub Desktop.
Ruby fizz buzz using pattern matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_fizz(num:) | |
return num % 3 == 0 ? 0b1 : 0b0 | |
end | |
def is_buzz(num:) | |
return num % 5 == 0 ? 0b1 : 0b0 | |
end | |
def fizz_buzz(max:) | |
(1..max).each do |num| | |
case (is_fizz(num: num) + is_buzz(num: num) * 2) | |
in 0b11 | |
puts "#{num} is FizzBuzz" | |
in 0b10 | |
puts "#{num} is Buzz" | |
in 0b01 | |
puts "#{num} is Fizz" | |
else | |
puts num | |
end | |
end | |
end | |
fizz_buzz(max: 50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment