Project

General

Profile

Actions

Bug #16192

closed

ENV.update hash values ignored when block given

Added by burdettelamar@yahoo.com (Burdette Lamar) over 4 years ago. Updated over 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[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

Updated by shevegen (Robert A. Heiler) over 4 years 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) over 4 years ago

  • Description updated (diff)

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

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

Updated by Eregon (Benoit Daloze) over 4 years ago

  • Tracker changed from Misc to Bug
  • Backport set to 2.5: UNKNOWN, 2.6: UNKNOWN

This is a clear bug, I'll merge @nobu's patch.
There are already specs for this from https://github.com/ruby/spec/pull/714, which I'll synchronize to MRI soon.

Actions #4

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

  • Status changed from Open to Closed

Applied in changeset git|5e0479f26afe1505afd9014ea96a206a88845828.


ENV.update should not call block on existing keys

[Bug #16192]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0