[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Can't bind a singleton method to a subclass?

David Walker

11/8/2006 2:39:00 AM

The behavior of this snippet doesn't make sense to me:

class A
def A.class_method
puts "Class method worked in #{self}"
end
end

class B < A; end

aklass = (class << A; self; end)
bklass = (class << B; self; end)

p RUBY_VERSION

A.class_method
B.class_method

p A.method(:class_method)
p B.method(:class_method)
p aklass.instance_method(:class_method)
p bklass.instance_method(:class_method)
p A.method(:class_method).unbind
p B.method(:class_method).unbind
p A.method(:class_method).unbind.bind(A)
p B.method(:class_method).unbind.bind(B)

The output (for me) is:

"1.8.4"
Class method worked in A
Class method worked in B
#<Method: A.class_method>
#<Method: B(A).class_method>
#<UnboundMethod: #<Class:A>#class_method>
#<UnboundMethod: #<Class:A>#class_method>
#<UnboundMethod: #<Class:A>#class_method>
#<UnboundMethod: #<Class:A>#class_method>
#<Method: A.class_method>
/workplace2/test.rb:24:in `bind': singleton method called for a
different object (TypeError)
from /workplace2/test.rb:24

In my mind, the last call should succeed, instead of raising an error.
Is it just plain impossible to re-bind the method to yield a #<Method:
B(A).class_method>?

--
=D ave

10 Answers

dblack

11/8/2006 2:46:00 AM

0

Ara.T.Howard

11/8/2006 3:01:00 AM

0

David Walker

11/8/2006 3:03:00 AM

0

On 11/7/06, dblack@wobblini.net <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? 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. :)

In any case, "B.method(:class_method)" gave me a #<Method:
B(A).class_method>. My original question still stands: is there no way
to recover this binding once it's been unbound?

--
=D ave

dblack

11/8/2006 3:36:00 AM

0

dblack

11/8/2006 3:39:00 AM

0

Ara.T.Howard

11/8/2006 4:23:00 AM

0

dblack

11/8/2006 4:44:00 AM

0

Ara.T.Howard

11/8/2006 4:51:00 AM

0

dblack

11/8/2006 2:59:00 PM

0

Ara.T.Howard

11/8/2006 3:46:00 PM

0