Project

General

Profile

Misc #16192

ENV.update hash values ignored when block given

Added by burdettelamar@yahoo.com (Burdette Lamar) about 2 months ago. Updated about 2 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
[ruby-core:95161]

Description

Doc:

update(hash) → Hash
update(hash) { |name, old_value, new_value| block } → Hash

Adds the contents of hash to the environment variables. If no block is specified entries with duplicate keys are overwritten, otherwise the value of each duplicate name is determined by calling the block with the key, its value from the environment and its value from the hash.

But:

The attached code shows that the block is called and its returned value assigned even if the name is not a duplicate. So basically this means that when a block is given, the hash values are ignored for all keys.

I had expected that for a non-duplicate name, the block would be ignored, and the hash value assigned.

I'm calling this a Misc, and not a Bug, just in case I'm missing something.


Files

t.rb (179 Bytes) t.rb burdettelamar@yahoo.com (Burdette Lamar), 09/30/2019 09:58 PM

History

Updated by shevegen (Robert A. Heiler) about 2 months ago

I can not say whether it is a bug or not, but I believe that either way,
it would be good to mention, in the documentation, which behaviour is to
be expected e. g. for the example you gave:

ENV.update('foo' => 'value_from_hash') { |name, old, new| 'value_from_block' }

If I understood the behaviour correctly then the block has a higher priority than
the (first) argument to .update(). Again, no idea if this is a bug or the
desired behaviour, but I think it would help if the documentation could
specify this and perhaps add a few examples to quickly demonstrate the use of
the method.

(Here is the link I used, if anyone else wants to look at it quickly:
https://ruby-doc.org/core-2.6.4/ENV.html#method-c-update )

Updated by nobu (Nobuyoshi Nakada) about 2 months ago

  • Description updated (diff)

I think this is a bug, for the same reason as [Bug #16173].

https://github.com/ruby/ruby/pull/2512

Also available in: Atom PDF