Destructure a Hash in block arguments in Ruby 2.7

I'm uncertain, but I think perhaps the idea is to use pattern matching for hash destructuring? For example:

{a: 1, b: 2}.tap do |args|
  args in {a: a, b: b} # !!!
  p a
end

Currently by default however, this will display a warning (which can be disabled):

Pattern matching is experimental, and the behavior may change in future versions of Ruby!


If you already know that you have two keys in each Hash as per your example, why not this?

[{a: 1, b: 2}, {a: 3, b: 4}].each do |h|
  a, b = h.values
  p a
end

Tags:

Ruby

Ruby 2.7