Project

General

Profile

Actions

Feature #6414

closed

Destructuring Assignment

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

Status:
Closed
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 2 (0 open2 closed)

Related to Ruby master - Feature #5474: keyword argumentClosedmame (Yusuke Endoh)10/23/2011Actions
Has duplicate Ruby master - Feature #8895: Destructuring Assignment for HashClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0