From: konstantin@... Date: 2018-03-08T11:05:58+00:00 Subject: [ruby-core:86046] [Ruby trunk Feature#14580] Hash#store accepts a block Issue #14580 has been updated by Soilent (Konstantin x). mame (Yusuke Endoh) wrote: > I think it is not so simple to optimize the double lookup by this API. Consider: > > hash.store(:a) {|val| 10000.times {|n| hash[n] = true }; val + 42 } > > or: > > hash.store(:a) {|val| hash.rehash; val + 42 } > > We need to keep a flag if rehash occurred or not during the block executed. I think that an exception should be thrown if the block modifies the hash. ---------------------------------------- Feature #14580: Hash#store accepts a block https://bugs.ruby-lang.org/issues/14580#change-70910 * 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: