Ruby Pattern Matching

For those of you using Ruby and miss pattern matching in Elixir, it looks like Ruby is adding some pattern matching functionality to Ruby 2.7 via case statements. Not exactly the same, but seems nice nonetheless!

Here’s an overview: https://medium.com/cedarcode/ruby-pattern-matching-1e84cab3b44a

case 0
in 0
  :match
end
#=> :match
case 0
in -1..1
  :match
end
#=> :match
case 0
in 3..6
  :no_match
end
#=> NoMatchingPatternError (0)
case 0
in Integer
  :match
end
#=> :match
case 0
in String
  :no_match
end
#=> NoMatchingPatternError (0)

case [0, 1, 2]
in [0, a, 3]
 :no_match       
end
#=> NoMatchingPatternError ([0, 1, 2])
case [0, 1, 2]
in [0, a, 2]
 a        
end
#=> 1
case [0, 1, 2]
in [0, *tail]
 tail  
end
#=> [1, 2]
4 Likes