Skip to content

Instantly share code, notes, and snippets.

@t0yohei
Created January 28, 2024 08:56
Show Gist options
  • Save t0yohei/04ae5784908bad3daf46fee60e69ad54 to your computer and use it in GitHub Desktop.
Save t0yohei/04ae5784908bad3daf46fee60e69ad54 to your computer and use it in GitHub Desktop.
Ruby fizz buzz using pattern matching
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