Understanding Comparison Operators Used to Determine Group_by Equivalence

Asked 2 years ago, Updated 2 years ago, 38 views

How to display aliases for specific methods

When I asked you a question, I found that group_by's equivalence decision was not == or eql? but I couldn't find the document when I looked for what was being used.

I searched ruby source code by def group_by, but found only test code...

https://github.com/ruby/ruby/blob/202bbda2bf5f25343e286099140fb9282880ecba/test/ruby/test_time_tz.rb#L68

Under what conditions does group_by determine that they are identical?

ruby

2022-09-30 14:12

1 Answers

If group_by is used to assemble a hash, it would be hash.

This is what happened when I tried it.

 irb(main): 043:0>a=Object.instance_method(:debugger)
=>#<UnboundMethod:Object(Kernel)#debugger(byebug)>
irb(main): 044:0>b=Object.instance_method(:byebug)
=>#<UnboundMethod:Object(Kernel)#byebug>
irb(main): 045:0>a==b
=>true
irb(main): 046:0>a.eql?(b)
=>true
irb(main): 047:0>a.hash==b.hash
=>false
irb(main): 048:0>Hash.instance_method(:has_key?).hash==Hash.instance_method(:member?).hash
=>true

The Object#hash documentation contains

If A.eql?(B), then A.hash==B.hash

You must meet the relationship.

It says, but the above behavior doesn't seem to be the same


2022-09-30 14:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.