Bug #1898
closedMethod#== for Methods with the Same Body
Description
=begin
The documentation for Method#== states "Two method objects are equal if that[sic] are bound to the same object and contain the same body." However, this doesn't seem to be true. Defining a method with either the same literal body or the same block (through define_method) doesn't create equal method objects. If this is intentional, could the documentation be clarified? It appears that methods are equal if:
-
They're bound to the same object, and
-
They have the same name, or,
-
They were designated as aliases with the alias keyword, or
-
They're a core method which is an alias.
Is this correct? (No patch because I'm not certain about how it's supposed to work).
=end
Updated by marcandre (Marc-Andre Lafortune) about 15 years ago
- Category changed from doc to core
- Assignee set to matz (Yukihiro Matsumoto)
- Priority changed from 3 to Normal
- Target version set to 1.9.2
=begin
I think you raise a couple of interesting questions, one of which due to the recent addition of respond_to_missing?
class A
def f1
42
end
def f2
42
end
alias_method :alias_f1, :f1
define_method :defined_f1, instance_method(:f1)
p = Proc.new { :bar }
define_method :proc1, p
define_method :proc2, p
define_method :block1, &p
define_method :block2, &p
def respond_to_missing? method
true
end
def method_missing method
:foo
end
end
a = A.new
I think it is clear that:¶
a.method(:alias_f1 ) == a.method(:f1 ) # ==> true
a.method(:defined_f1 ) == a.method(:f1 ) # ==> true
a.method(:missing ) == a.method(:not_here) # ==> false
and, although they were defined similarly:¶
a.method(:f2 ) == a.method(:f1 ) # ==> false
OK so far. The following currently return false, but.¶
it might be preferable if they returned true. What do you think?¶
a.method(:missing ) == a.method(:missing ) # ==> ???
a.method(:proc1 ) == a.method(:proc2 ) # ==> ???
a.method(:block1 ) == a.method(:block2 ) # ==> ???
a.method(:proc1 ) == a.method(:block1 ) # ==> ???
=end
Updated by matz (Yukihiro Matsumoto) about 15 years ago
=begin
Hi,
In message "Re: [ruby-core:25755] [Bug #1898] Method#== for Methods with the Same Body"
on Fri, 25 Sep 2009 06:08:40 +0900, Marc-Andre Lafortune redmine@ruby-lang.org writes:
|# OK so far. The following currently return false, but.
|# it might be preferable if they returned true. What do you think?
|a.method(:proc1 ) == a.method(:proc2 ) # ==> ???
|a.method(:block1 ) == a.method(:block2 ) # ==> ???
|a.method(:proc1 ) == a.method(:block1 ) # ==> ???
Those makes sense. I will change the methods from same proc/block to
be equal.
|a.method(:missing ) == a.method(:missing ) # ==> ???
They are proc objects generated on-the-fly, so that it's difficult to
make them equal.
matz.
=end
Updated by matz (Yukihiro Matsumoto) about 15 years ago
- Status changed from Open to Closed
- % Done changed from 0 to 100
=begin
Applied in changeset r25107.
=end