Feature #2117

Binding to a class, a method from the class's superclass's metaclass, fails

Added by duairc (Shane O'Brien) over 2 years ago. Updated about 1 year ago.

[ruby-core:25632]
Status:Closed Start date:09/18/2009
Priority:Normal Due date:
Assignee:matz (Yukihiro Matsumoto) % Done:

0%

Category:-
Target version:-

Description

I guess the best way to explain this is with an example:

>> class A; def self.foo; 'bar'; end; end
=> nil
>> class B < A; end
=> nil
>> A.method(:foo).unbind.bind(B)
TypeError: singleton method called for a different object
        from (irb):3:in `bind'
        from (irb):3
        from /usr/bin/irb1.9:12:in `<main>'

I've tried this on Ruby 1.8.5, 1.8.6, 1.8.7, 1.9.0 and 1.9.1 and I get this behaviour. It works in JRuby however. It's quite trivial to fix. I've attached a patch below, after the application of which Ruby's behaviour is:

>> class A; def self.foo; 'bar'; end; end
=> nil
>> class B < A; end
=> nil
>> A.method(:foo).unbind.bind(B).call
=> "bar"
>> a = A.new
=> #<A:0x00000000b1d870>
>> b = B.new
=> #<B:0x00000000b18230>
>> a.define_singleton_method(:foo){'baz'}
=> #<Proc:0x00000000b10df0@(irb):6 (lambda)>
>> a.method(:foo).unbind.bind(b)
TypeError: singleton method called for a different object
	from (irb):7:in `bind'
	from (irb):7
	from ./irb:12:in `<main>'

I think this makes more sense.

fix-class-meta-class-superclass-bind.patch (1.6 kB) duairc (Shane O'Brien), 09/18/2009 07:57 pm

History

Updated by marcandre (Marc-Andre Lafortune) over 2 years ago

  • Category set to core
  • Assignee set to marcandre (Marc-Andre Lafortune)

Updated by marcandre (Marc-Andre Lafortune) over 2 years ago

  • Status changed from Open to Closed
Thank you for your patch.
Applied in http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=25046

Also fixed 1.8
http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=25047

Rubyspecs updated.

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

  • Status changed from Closed to Assigned
  • Assignee changed from marcandre (Marc-Andre Lafortune) to matz (Yukihiro Matsumoto)

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

Hi,

At Wed, 23 Sep 2009 06:04:42 +0900,
Marc-Andre Lafortune wrote in [ruby-core:25719]:
> Thank you for your patch.
> Applied in http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=25046
> 
> Also fixed 1.8
> http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=25047
> 
> Rubyspecs updated.

I've thought it was intentional behavior and the patch changes
the spec, though I don't object against the change.

-- 
Nobu Nakada

Updated by marcandre (Marc-Andre Lafortune) over 2 years ago

Hi!
   Yes, I hesitated before making the change but my reasoning was:
- past behavior contradicted the doc & intuition (at least mine! ;-)
- no apparent valid reason for past behavior (either for MRI or the language)
- possibly previous behavior was in thinking of singleton classes of objects, maybe because Ruby 1.8's official inheritance for singleton classes is just wrong (see #567 )
- fixing the bug wouldn't cause any compatibility issue; I could not see how one would rely on the #bind or #define_method failing, and if it didn't that it would be a problem.

I'll undo the changes if this is incorrect though.

Updated by mame (Yusuke Endoh) about 2 years ago

  • Status changed from Assigned to Closed
Hi,

2009/9/24 Marc-Andre Lafortune <redmine@ruby-lang.org>:
> Hi!
> ? Yes, I hesitated before making the change but my reasoning was:
> - past behavior contradicted the doc & intuition (at least mine! ;-)
> - no apparent valid reason for past behavior (either for MRI or the language)
> - possibly previous behavior was in thinking of singleton classes of objects, maybe because Ruby 1.8's official inheritance for singleton classes is just wrong (see #567 )
> - fixing the bug wouldn't cause any compatibility issue; I could not see how one would rely on the #bind or #define_method failing, and if it didn't that it would be a problem.
>
> I'll undo the changes if this is incorrect though.


Any objection has not been expressed.  And I personally agree
with Marc-Andre.

So I close this ticket.  If anyone has an objection, please
reopen the ticket by the end of this month.

-- 
Yusuke ENDOH <mame@tsg.ne.jp>

Also available in: Atom PDF