Issue #14580 has been updated by shevegen (Robert A. Heiler).


If I understood your proposal correctly then you want an
additional way to update an existing value in a hash, correct?

So the comparable syntax parts would be:

    hash[:a] = hash[:a] + 42

versus

    hash.store(:a) { |val| val + 42 }

right?

So, if this is correct, then as I understand it, the major
point of your proposal, and benefit, is that you omit querying
the old value in the sedond variant, since you just operate
on the block variable called "val" in your case.

If this is indeed the case, and that is your proposal, then I
think I understand what you mean, feature-wise. In this case
you skip the step where you query the old value explicitely
and just tap into the block value for making a modification.

I think this is ok.

I have no idea how matz feels about it; perhaps someone could
suggest it in the upcoming ruby developer meeting in ~a 
week or so.

The current documentation for Hash#store can be found at:

https://docs.ruby-lang.org/en/2.5.0/Hash.html#method-i-store

----------------------------------------
Feature #14580: Hash#store accepts a block
https://bugs.ruby-lang.org/issues/14580#change-70840

* Author: Soilent (Konstantin x)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Given a hash

~~~ ruby
hash = { a: 2 }
~~~

I want to update a single value in the hash:

~~~ ruby
hash[:a] = hash[:a] + 42
hash[:a] #=> 44
~~~

But instead, I would like to have a method that yields the current value for a given key and associates the block result with the key (similar to Hash#update). I think that Hash#store can be extended to support a block arg.

~~~ ruby
hash.store(:a) { |val| val + 42 }
hash[:a] #=> 44
~~~

Or it can be something like this:

~~~ ruby
hash.transform_values(:a, :b) { |val| val + 42 }
hash[:a] #=> 44
~~~



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>