ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "jeremyevans0 (Jeremy Evans)" <noreply@ruby-lang.org>
To: ruby-core@neon.ruby-lang.org
Subject: [ruby-core:110816] [Ruby master Bug#19113] Inconsistency in retention of compare_by_identity flag in Hash methods
Date: Fri, 18 Nov 2022 18:57:38 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-100174.20221118185737.1604@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19113.20221109000421.1604@ruby-lang.org

Issue #19113 has been updated by jeremyevans0 (Jeremy Evans).


headius (Charles Nutter) wrote in #note-6:
> Even though it's a "new Hash", it is supposed to be populated with "the given objects". If losing identity comparison means some keys don't get populated, I would consider that broken.

If you turn the hash into an array, you have the same `given objects`, but different results:

```ruby
h = {'a'=>1}.compare_by_identity
h['a'] = 2
Hash[h]      # {'a'=>1, 'a'=>2}.compare_by_identity
Hash[h.to_a] # {'a'=>2}
```

Not respecting the compare_by_identity flag (as my PR does) would make `Hash[h] == Hash[h.to_a]`.

I would say that `populated with the given objects` does not imply `resulting in the same set of keys.`  Also, `given objects` (not `given object`) implies it looks at the content of the hash, but not the hash object itself (hence why default value/proc is not copied).

Here's the pseudocode I would consider based on the documentation:

```ruby
# new hash
output_hash = {}
# populated with the given objects
input_hash_or_array.each do |k, v|
  output_hash[k] = v
end
output_hash
```

There are certainly valid arguments for both sides, so we'll have to see what @matz decides. 

----------------------------------------
Bug #19113: Inconsistency in retention of compare_by_identity flag in Hash methods
https://bugs.ruby-lang.org/issues/19113#change-100174

* Author: jeremyevans0 (Jeremy Evans)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.2.0dev (2022-11-07T17:29:28Z master 9001e53e68) [x86_64-openbsd7.2]
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
`Hash.[]` and `Hash.ruby2_keywords_hash` retain the compare_by_identity flag for non-empty hashes, but do not retain it for empty hashes:

```ruby
hs = [{}.compare_by_identity, {:a=>1}.compare_by_identity]
hs.map{|h| Hash[h].compare_by_identity?}
# => [false, true]
hs.map{|h| Hash.ruby2_keywords_hash(h).compare_by_identity?}
# => [false, true]
```

This inconsistency seems like a bug.

`Hash#compact` always drops the compare_by_identity flag, but it is documented as returning a copy of self, implying the compare_by_identity flag is kept (since #dup and #clone retain the flag).

```ruby
{}.compare_by_identity.compact.compare_by_identity?
# => false
```

I'm not sure whether is a bug, because it is consistent, but I think retaining the flag makes more sense.

I'll try to work on a fix for both of these issues tomorrow.



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

  parent reply	other threads:[~2022-11-18 18:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-09  0:04 [ruby-core:110663] [Ruby master Bug#19113] Inconsistency in retention of compare_by_identity flag in Hash methods jeremyevans0 (Jeremy Evans)
2022-11-09 20:21 ` [ruby-core:110672] " jeremyevans0 (Jeremy Evans)
2022-11-15  7:38 ` [ruby-core:110759] " ko1 (Koichi Sasada)
2022-11-18 17:03 ` [ruby-core:110812] " headius (Charles Nutter)
2022-11-18 17:03 ` [ruby-core:110813] " headius (Charles Nutter)
2022-11-18 17:32 ` [ruby-core:110814] " jeremyevans0 (Jeremy Evans)
2022-11-18 18:37 ` [ruby-core:110815] " headius (Charles Nutter)
2022-11-18 18:57 ` jeremyevans0 (Jeremy Evans) [this message]
2022-11-18 19:47 ` [ruby-core:110817] " Eregon (Benoit Daloze)
2022-12-23  2:39 ` [ruby-core:111389] " matz (Yukihiro Matsumoto) via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-100174.20221118185737.1604@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=ruby-core@neon.ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).