Literal Matches

`case 0in 0 then trueelse falseend# => true`

Multiple Matchers

`case 0in 0 | 1  trueend# => true`
`case 0in a | 0end# Error: illegal variable in alternative pattern`

Captured Variables

`case truein a  aend# => true`
`case 0in a if a == 0  trueend# => true`
`case 0in 0 => a  a == 0end`
`case 0in _ | _a  trueend`

Destructuring

`case [0, 1]in a, a  a == 1end# => true`
`class C  class << self    attr_accessor :keys  end  def initialize(obj)    @obj = obj  end  def deconstruct    @obj  end  def deconstruct_keys(keys)    C.keys = keys    @obj  endend`
`[[0, 1], C.new([0, 1])].all? do |i|  case i  in 0, 1    true  endend`
`[[0], C.new([0])].all? do |i|  case i  in 0, 1  else    true  endend`
`case []in *a  a == []endcase [0, 1, 2]in *a, 1, 2  a == [0]end`
`case 0in *  trueend`
`case {a: 0}in a: 0  trueend`
`case {}in **a  a == {}end`

Triple Equals

`case 'abc'in /a/  trueendcase 0in -> i { i == 0 }  trueend`
`case [0, 1, 2, 3, 4, 5]in [0..1, 0...2, 0.., 0..., (...5), (..5)]  trueend`
`case objectin method_a: 0..10, method_b: String  trueend`

Pin Operator

`a = /a/case 'abc'in ^a  trueendcase [0, 0]in a, ^a  a == 0end`

Thoughts?

--

--

--

More from Brandon Weaver

Find new posts on dev.to: https://dev.to/baweaver

Love podcasts or audiobooks? Learn on the go with our new app.

Brandon Weaver

Find new posts on dev.to: https://dev.to/baweaver