From: "chendo (Jack Chen)" Date: 2013-09-12T09:54:44+09:00 Subject: [ruby-core:57151] [ruby-trunk - Feature #8895] Destructuring Assignment for Hash Issue #8895 has been updated by chendo (Jack Chen). sawa (Tsuyoshi Sawada) wrote: > =begin > Given that destructive assignments with array prohibits the `[ ]` on the left side of the assignment, that is: > > a, b = [1, 2] > > instead of: > > [a, b] = [1, 2] > > it would be more consistent if your proposal were: > > name: name, age: age = {name: "John Smith", age: 42} > > rather than: > > {name: name, age: age} = {name: "John Smith", age: 42} > =end I left the braces in because I felt it would be easier to parse, however if without braces is doable as well, that would work also. Will update the proposal. ---------------------------------------- Feature #8895: Destructuring Assignment for Hash https://bugs.ruby-lang.org/issues/8895#change-41763 Author: chendo (Jack Chen) Status: Open Priority: Normal Assignee: Category: Target version: =begin Given Ruby already supports destructuring assignment with Array (a, b = [1, 2]), I propose destructuring assignments for Hash. == Basic example 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 { => , ��� } = # Symbols { foo: bar } = { foo: "bar" } bar == "bar" # Potential shorthand { foo } = { foo: "bar" } foo == "bar" == Use cases: # MatchData { username: username, age: age } = "user:jsmith age:42".match(/user:(?\w+) age:(?\d+)/) username == "jsmith" age == "42" == Edge cases # Variable being assigned to more than once should use the last one { foo: var, bar: var } = {foo: 1, bar: 2} var == 2 Thoughts? =end -- http://bugs.ruby-lang.org/