Project

General

Profile

Feature #6414

Destructuring Assignment

Added by edtsech (Edward Tsech) over 8 years ago. Updated about 3 years ago.

Status:
Feedback
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:44951]

Description

I mean:

john = {name: "John", last: "Smith"}
{name, last} = john # extract value by key from the hash and set to local variable with same name
name # => "John"
last # -> "Smith"

Useful for ex. for "options" hashes:
def select(options={})
{only, except} = options
...
end

As extra part of this feature can be hash constructing in this way:

name = "John"
last = "Smith"
find({name, last}) # equals to => find({name: "John", last: "Smith"})

I think both really nice to have in Ruby.
Thanks.


Files

destructuring.pdf (128 KB) destructuring.pdf marcandre (Marc-Andre Lafortune), 07/01/2012 06:26 AM

Related issues

Related to Ruby master - Feature #5474: keyword argumentClosedmame (Yusuke Endoh)10/23/2011Actions

Also available in: Atom PDF