From: "chendo (Jack Chen)" Date: 2013-09-12T09:52:54+09:00 Subject: [ruby-core:57150] [ruby-trunk - Feature #8895] Destructuring Assignment for Hash Issue #8895 has been updated by chendo (Jack Chen). marcandre (Marc-Andre Lafortune) wrote: > I suggested something similar in [ruby-core:41772]. > Here is a summary from my similar suggestion made in [ruby-core:41772]: > > {key: 'default', other_key:, **other_options} = {other_key: 42, foo: 'bar'} > key # => 'default' > other_key # => 42 > other_options # => {foo: 'bar'} > > You'll note that it doesn't give the possibility to map the key to a different variable. Indeed, I don't think that it would be useful and I would rather encourage rubyists to use meanginful option and variable names. It also makes very similar to the way we declare keyword arguments for methods, so no additional learning curve. Your proposal is quite different. I considered the case of default options, but I couldn't figure out a way to make it read well, and there are many cases where the keys in the hash are not symbols. No value variable after other_key: feels a bit off to me, too. I'm all for a way to figure out how to get the use case of default options in somehow but I feel that needs more consideration where as this is useful by itself. ---------------------------------------- Feature #8895: Destructuring Assignment for Hash https://bugs.ruby-lang.org/issues/8895#change-41762 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/