Ruby Rightward Assignment -> JS-like Destructuring
It's not often there's a javascript feature I wish was available in ruby, but here we are. But, it turns out ruby has the functionality as of 2.7 and I was just out of the loop.
In javascript you can use destructuring assignment to unpack a bunch of variables in a single line:
const obj = { a: 1, b: 2, c: 3, d: 4 }
const { a, b, d: newName } = obj
console.log([a, b, newName])
// => [1, 2, 4]
With rightward assignment you can do a similar thing with hashes, though with slightly different syntax:
hsh = { a: 1, b: 2, c: 3, d: 4 }
hsh => { a:, b:, d: new_name }
puts [a, b, new_name]
# => [1, 2, 4]
Nice!
Tweet