When I work in Ruby, I really miss the pattern matching of Elixir. Today I discovered a few restructuring tricks for hashes that recreate some of that pattern matching goodness from Elixir. The TLDR is that you can use forward assignment.
options = {one: 1, two: 2, optional: false}
# then later
options => {one:, two:, optional:}
$> one
1
$> optional
false
Note the => rightward operator, aka Ruby’s old friend the hashrocket, this is called forward assignment.
You can make the above more robust with a rescue clause:
options => {one:, two:, optional:} rescue nil
In the event that somebody passes in an options hash like {one: 1, two: 2} this will prevent things from blowing up. When destructuring Ruby will return nil for optional.