Project

General

Profile

Actions

Feature #11690

open

Update Hash during multiple assignment

Added by danielpclark (Daniel P. Clark) about 9 years ago. Updated almost 9 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:71495]

Description

Given that we can assign multiple variables at once

a,b,c = 1,2,3

It would be nice to be able to update a Hash during multiple assignment rather than replacing it. Currently

x = {a: 1, b: 2}
x, y ,z = {c: 3}, 6, 7
x
# => {c: 3}

What I propose is adding Hash#update= to permit updating during multiple assignment.

class Hash
  def update=(h)
    update(h)
  end
end

x = {a: 1, b: 2}

x.update, y ,z = {c: 3}, 6, 7
x
# => {a: 1, b: 2, c: 3}

This would be most useful in scenarios where a method or proc return multiple values. When the method returns the values we don't normally know the key outside where the hash assignment is.

example = proc { [{:hi => :hello}, 5] }

hash = {}

# Currently in Ruby with an Unknown key multiple assignment isn't an option
hash[????], current = example.call

# We currently have to two step it
result, current = example.call
hash.update(result)

But with Hash#update= we don't have to know the key.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0