ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:106048] [Ruby master Bug#18334] ENV#to_h returns a new Hash object but Hash#to_h does not, which can cause inconsistencies
@ 2021-11-14  5:08 postmodern (Hal Brodigan)
  2021-11-14 12:23 ` [ruby-core:106049] " Eregon (Benoit Daloze)
  2021-11-14 15:46 ` [ruby-core:106052] " postmodern (Hal Brodigan)
  0 siblings, 2 replies; 3+ messages in thread
From: postmodern (Hal Brodigan) @ 2021-11-14  5:08 UTC (permalink / raw
  To: ruby-core

Issue #18334 has been reported by postmodern (Hal Brodigan).

----------------------------------------
Bug #18334: ENV#to_h returns a new Hash object but Hash#to_h does not, which can cause inconsistencies
https://bugs.ruby-lang.org/issues/18334

* Author: postmodern (Hal Brodigan)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.1.0dev (2021-11-13T20:48:57Z master fc456adc6a) [x86_64-linux]
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
I noticed that in ruby-3.1.0-preview1 ENV#dup was removed in favor of ENV#to_h. However, methods that accept either ENV or a Hash object will not behave the same, since ENV#to_h returns a new Hash object but Hash#to_h returns the same Hash instance.

    ENV.to_h.object_id
    # => 14700
    ENV.to_h.object_id
    # => 14760
    hash = {"FOO" => "bar"}
    hash.to_h.object_id
    # => 14820
    hash.to_h.object_id
    # => 14820

I propose that the ENV#dup method be re-added as `self.to_h.dup`.



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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:106049] [Ruby master Bug#18334] ENV#to_h returns a new Hash object but Hash#to_h does not, which can cause inconsistencies
  2021-11-14  5:08 [ruby-core:106048] [Ruby master Bug#18334] ENV#to_h returns a new Hash object but Hash#to_h does not, which can cause inconsistencies postmodern (Hal Brodigan)
@ 2021-11-14 12:23 ` Eregon (Benoit Daloze)
  2021-11-14 15:46 ` [ruby-core:106052] " postmodern (Hal Brodigan)
  1 sibling, 0 replies; 3+ messages in thread
From: Eregon (Benoit Daloze) @ 2021-11-14 12:23 UTC (permalink / raw
  To: ruby-core

Issue #18334 has been updated by Eregon (Benoit Daloze).


I think it would be confusing if `ENV#dup` returns a `Hash` and not a `ENV`, as that would be inconsistent with all other `#dup`.

`to_SHORT` never guarantees to return a copy, so if you don't know the receiver and want to mutate it, you should probably use `obj.to_h.dup`, which already works fine.

----------------------------------------
Bug #18334: ENV#to_h returns a new Hash object but Hash#to_h does not, which can cause inconsistencies
https://bugs.ruby-lang.org/issues/18334#change-94636

* Author: postmodern (Hal Brodigan)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.1.0dev (2021-11-13T20:48:57Z master fc456adc6a) [x86_64-linux]
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
I noticed that in ruby-3.1.0-preview1 ENV#dup was removed in favor of ENV#to_h. However, methods that accept either ENV or a Hash object will not behave the same, since ENV#to_h returns a new Hash object but Hash#to_h returns the same Hash instance.

    ENV.to_h.object_id
    # => 14700
    ENV.to_h.object_id
    # => 14760
    hash = {"FOO" => "bar"}
    hash.to_h.object_id
    # => 14820
    hash.to_h.object_id
    # => 14820

I propose that the ENV#dup method be re-added as `self.to_h.dup`.



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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:106052] [Ruby master Bug#18334] ENV#to_h returns a new Hash object but Hash#to_h does not, which can cause inconsistencies
  2021-11-14  5:08 [ruby-core:106048] [Ruby master Bug#18334] ENV#to_h returns a new Hash object but Hash#to_h does not, which can cause inconsistencies postmodern (Hal Brodigan)
  2021-11-14 12:23 ` [ruby-core:106049] " Eregon (Benoit Daloze)
@ 2021-11-14 15:46 ` postmodern (Hal Brodigan)
  1 sibling, 0 replies; 3+ messages in thread
From: postmodern (Hal Brodigan) @ 2021-11-14 15:46 UTC (permalink / raw
  To: ruby-core

Issue #18334 has been updated by postmodern (Hal Brodigan).


With the current implementation of ENV#to_h, `ENV.to_h.dup` would create two new Hashes instead of one. `hash.to_h.dup` would correctly create only one new Hash.

----------------------------------------
Bug #18334: ENV#to_h returns a new Hash object but Hash#to_h does not, which can cause inconsistencies
https://bugs.ruby-lang.org/issues/18334#change-94639

* Author: postmodern (Hal Brodigan)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.1.0dev (2021-11-13T20:48:57Z master fc456adc6a) [x86_64-linux]
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
I noticed that in ruby-3.1.0-preview1 ENV#dup was removed in favor of ENV#to_h. However, methods that accept either ENV or a Hash object will not behave the same, since ENV#to_h returns a new Hash object but Hash#to_h returns the same Hash instance.

    ENV.to_h.object_id
    # => 14700
    ENV.to_h.object_id
    # => 14760
    hash = {"FOO" => "bar"}
    hash.to_h.object_id
    # => 14820
    hash.to_h.object_id
    # => 14820

I propose that the ENV#dup method be re-added as `self.to_h.dup`.



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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-11-14 15:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-14  5:08 [ruby-core:106048] [Ruby master Bug#18334] ENV#to_h returns a new Hash object but Hash#to_h does not, which can cause inconsistencies postmodern (Hal Brodigan)
2021-11-14 12:23 ` [ruby-core:106049] " Eregon (Benoit Daloze)
2021-11-14 15:46 ` [ruby-core:106052] " postmodern (Hal Brodigan)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).