From: dblack@... Date: 2006-11-08T12:36:01+09:00 Subject: Re: Can't bind a singleton method to a subclass? Hi -- On Wed, 8 Nov 2006, David Walker wrote: > On 11/7/06, dblack@wobblini.net wrote: >> The fact that B can call A.class_method is due to the special-casing >> of singleton classes of Class objects; it's the one case where a given >> object can execute singleton methods of another object. But those >> singleton methods still belong, unambiguously, to the first object, A. >> The reason B can call them is that A's singleton class serves as the >> superclass of B's singleton class -- so class_method lies on the >> method look-up path of B. But class_method is still defined only in >> A's singleton class. > > Thanks for the info. That's mostly what I thought - I guess I should > have said, "This seems silly to me," not, "This doesn't make sense to > me." :) > > How, though, is this a special case? What I mean is: the fact that you can call B.class_method, even though class_method is a singleton method of an object other than B, is a special dispensation for class objects. > For instance methods, you can > bind an instance method to any object who's class is <= the class the > method is defined on. Why should it be different for singletons? I.e. > I have an instance method (instance from the perspective of the > singleton class A), I should be able to bind it to an object who's > class is a subclass of the class the instance is defined on. If A's > singleton class is a superclass of B's singleton class (which it ought > to be) then this relation holds. I think. :) I guess it's just a matter of how the logic cascades. Forbidding a rebinding of a singleton method on a different object takes precedence over the special situation where class singleton methods are sort of not really singleton (i.e., can be called by subclasses). > In any case, "B.method(:class_method)" gave me a # B(A).class_method>. My original question still stands: is there no way > to recover this binding once it's been unbound? Maybe, but not that I can think of. David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org