Object Destructuring

I come from a JavaScript background and in JavaScript you are able to destructure an object to create a variable with the value of the key of an object.

const {name, age} = {name: "Steve", age: 33}
console.log(name)  // logs "Steve"
console.log(age) // logs 33

I know you are able to do this with an array in Ruby. Are you somehow able to do this with an Object/Hash in Ruby?

If you search for “parallel assignment”, you’ll find what you’re looking for. In ruby, you don’t need to destructure to make it work.
two examples:
a, b = 1,2
hash1 = {}
hash1[:fruit], hash1[:veggie] = "apple", "carrot"

1 Like