The Crystal Programming Language Forum

Pattern matching in Ruby 2.7.0

Recently Ruby has added pattern matching:

require "json"

json = <<END
{
  "name": "Alice",
  "age": 30,
  "children": [{ "name": "Bob", "age": 2 }]
}
END

case JSON.parse(json, symbolize_names: true)
in {name: "Alice", children: [{name: "Bob", age: age}]}
  p age #=> 2
end

It can deconstruct the inside of a “structure”.

Will Crystal consider support for pattern matching in the future?

2 Likes

I wrote a library to match inside values and support deconstruction.

1 Like