[ruby-core:109602] [Ruby master Feature#18798] `UnboundMethod#==` with inherited classes
From:
"Eregon (Benoit Daloze)" <noreply@...>
Date:
2022-08-20 19:48:14 UTC
List:
ruby-core #109602
Issue #18798 has been updated by Eregon (Benoit Daloze).
I think we should do this, because I think the main purpose of {Method,UnboundMethod}#== is to find out if two methods have the same definition (or in other words, are equivalent in behavior).
I filed #18969 which is a duplicate.
That issue also suggests that if changing {Method,UnboundMethod}#== is not acceptable, then a new method could do that e.g. `{Method,UnboundMethod}#same_definition?(other)`.
----------------------------------------
Feature #18798: `UnboundMethod#==` with inherited classes
https://bugs.ruby-lang.org/issues/18798#change-98785
* Author: ko1 (Koichi Sasada)
* Status: Open
* Priority: Normal
----------------------------------------
Now `UnboundMethod` for a same method from a superclass and an inherited class are not `==`.
```ruby
class C
def foo = :C
$mc = instance_method(:foo)
end
class D < C
$md = instance_method(:foo)
end
p $mc == $md #=> false
p $mc.owner #=> C
p $mc.owner == $md.owner #=> true
p $mc.source_location == $md.source_location #=> true
p $mc.inspect #=> "#<UnboundMethod: C#foo() t.rb:3>"
p $md.inspect #=> "#<UnboundMethod: D(C)#foo() t.rb:3>"
```
How about to make it `UnboundMethod#==` return true for this case?
Rule: "return true if the UnboundMethod objects point to a same method definition" seems simple.
FYI: On aliased unbound methods point to a same method are `==`.
```ruby
class C
def foo = :C
alias bar foo
$mfoo = instance_method(:foo)
$mbar = instance_method(:bar)
end
p $mfoo, $mbar
#=> #<UnboundMethod: C#foo() t.rb:2>
#=> #<UnboundMethod: C#bar(foo)() t.rb:2>
p $mfoo == $mbar #=> true
```
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>