Project

General

Profile

Actions

Feature #18798

open

`UnboundMethod#==` with inherited classes

Added by ko1 (Koichi Sasada) about 1 month ago. Updated about 1 month ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:108659]

Description

Now UnboundMethod for a same method from a superclass and an inherited class are not ==.

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 ==.

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
Actions #1

Updated by ko1 (Koichi Sasada) about 1 month ago

  • Description updated (diff)

Updated by sawa (Tsuyoshi Sawada) about 1 month ago

Did you mean:

p $mc.owner == $md.owner #=> true
p $mc.source_location == $md.source_location #=> true

I think the proposal is a good idea.

Updated by jeremyevans0 (Jeremy Evans) about 1 month ago

I'm not against this change (for UnboundMethod, I think Method should remain different), but it seems more like a feature request than a bug fix to me.

Updated by Eregon (Benoit Daloze) about 1 month ago

+1. @ko1 (Koichi Sasada) I guess you meant this as a feature request?

Updated by Eregon (Benoit Daloze) about 1 month ago

Regarding Method#==, I think it should also respects the simple rule "point to a same method definition" + ensure the receiver is the same object for both Method instances.

Updated by ko1 (Koichi Sasada) about 1 month ago

  • Backport deleted (2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN)
  • ruby -v deleted (ruby 3.2.0dev (2022-01-14T04:46:12Z gh-4636 c613d79f9b) [x64-mswin64_140])
  • Tracker changed from Bug to Feature

Ah, yes, it is a feature request.

Updated by ko1 (Koichi Sasada) about 1 month ago

  • Description updated (diff)

ah, mistake...

Actions

Also available in: Atom PDF