Skip to content

Instantly share code, notes, and snippets.

@seban
Created April 18, 2019 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seban/583e3ff2335d975ce21b9cb09773fe9f to your computer and use it in GitHub Desktop.
Save seban/583e3ff2335d975ce21b9cb09773fe9f to your computer and use it in GitHub Desktop.
First try of Ruby 2.7 pattern matching
def fizzbuzz(number)
case [number % 3, number % 5]
in [0, 0] then 'FizzBuzz'
in [0, _] then 'Fizz'
in [_, 0] then 'Buzz'
end
end
puts fizzbuzz(15) # => 'FizzBuzz'
puts fizzbuzz(3) # => 'Fizz'
puts fizzbuzz(5) # => 'Buzz'
puts fizzbuzz(4) # => NoMatchingPatternError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment