Project

General

Profile

Bug #16164

ENV.each with block returns ENV object itself, not Hash

Added by burdettelamar@yahoo.com (Burdette Lamar) 11 days ago. Updated 2 days ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.3p62 (2019-04-16 revision 67580) [x64-mingw32]
[ruby-core:94908]

Description

My code:

x = ENV.each { |name, value| }
puts "Return value class: #{x.class}"
puts "Return value is a Hash? #{x.kind_of?(Hash)}"
puts "Return value is actually ENV itself? #{x.object_id == ENV.object_id}"

Its output:

Return value class: Object
Return value is a Hash? false
Return value is actually ENV itself? true

I expected a Hash, because the doc says:

each { |name, value| block } → Hash

There 's some danger here, because if I modify the returned object, I'm actually modifying ENV itself.

I think this is also true for a number of other methods in ENV; e.g., ENV.delete_if.


Files

diff.txt (1.17 KB) diff.txt burdettelamar@yahoo.com (Burdette Lamar), 09/20/2019 06:50 PM

Associated revisions

Revision 27144de2
Added by jeremyevans (Jeremy Evans) 2 days ago

Fix documentation for ENV.each to return ENV

Also have spec check that it returns ENV.

Mostly from burdettelamar@yahoo.com (Burdette Lamar).

Fixes [Bug #16164]

History

Updated by sawa (Tsuyoshi Sawada) 11 days ago

That is a documentation bug.

Updated by shevegen (Robert A. Heiler) 11 days ago

I remember the old pickaxe having mentioned ENV as a hash-like
object. I guess the documentation could be updated to include
e. g. specifically a .is_a? Hash check too, and explain that
ENV is not completely a "full" Hash.

Updated by burdettelamar@yahoo.com (Burdette Lamar) 2 days ago

I'm attaching a diff that:

  • Enhances the test to verify the returned object, which is ENV.
  • Corrects the documentation to say that the returned object is ENV, not a Hash.
#4

Updated by jeremyevans (Jeremy Evans) 2 days ago

  • Status changed from Open to Closed

Applied in changeset git|27144de2bd95c5886c3d4400c3b8aea932b856f1.


Fix documentation for ENV.each to return ENV

Also have spec check that it returns ENV.

Mostly from burdettelamar@yahoo.com (Burdette Lamar).

Fixes [Bug #16164]

Also available in: Atom PDF