From: ruby-core@... Date: 2014-07-14T10:02:30+00:00 Subject: [ruby-core:63711] [ruby-trunk - Feature #8895] Destructuring Assignment for Hash Issue #8895 has been updated by Marc-Andre Lafortune. Sean Linsley wrote: > I don't follow. Can't this assignment behave the same way that method argument destructuring does? I agree. Destructuring should work as method & block passing works (apart from block passing) ---------------------------------------- Feature #8895: Destructuring Assignment for Hash https://bugs.ruby-lang.org/issues/8895#change-47761 * Author: Jack Chen * Status: Open * Priority: Normal * Assignee: * Category: * Target version: ---------------------------------------- Given Ruby already supports destructuring assignment with Array (a, b = [1, 2]), I propose destructuring assignments for Hash. Basic example ------------- ```ruby params = {name: "John Smith", age: 42} {name: name, age: age} = params # name == "John Smith" # age == 42 ``` This would replace a common pattern of assigning hash values to local variables to work with. General syntax -------------- ```ruby { => , ��� } = # Symbols { foo: bar } = { foo: "bar" } bar == "bar" # Potential shorthand { foo } = { foo: "bar" } foo == "bar" ``` Use cases --------- ```ruby # MatchData { username: username, age: age } = "user:jsmith age:42".match(/user:(?\w+) age:(?\d+)/) username == "jsmith" age == "42" ``` Edge cases ---------- ```ruby # Variable being assigned to more than once should use the last one { foo: var, bar: var } = {foo: 1, bar: 2} var == 2 ``` Thoughts? -- https://bugs.ruby-lang.org/