From: dblack@... Date: 2006-11-08T13:43:39+09:00 Subject: Re: Can't bind a singleton method to a subclass? Hi -- On Wed, 8 Nov 2006, ara.t.howard@noaa.gov wrote: > On Wed, 8 Nov 2006 dblack@wobblini.net wrote: > >> I think it's not so much that you're unbinding it *from* B, as that >> you're >> grabbing it *through* B. So, by the time you unbind it, it doesn't >> "know" >> that you got it that way; it's just a Method object, and it allows itself >> to >> be unbound. > > yeah. at first i thought that, but: > > harp:~ > cat a.rb > class A > def self.class_method() 42 end > end > class B < A; end > > p A.method(:class_method) > p B.method(:class_method) > > > harp:~ > ruby a.rb > # > # > > so it seems like the method is aware that it's receiver is B. but i take > your point. Interesting. The dot is the singleton-method indicator in that notation: irb(main):013:0> obj = Object.new => # irb(main):014:0> def obj.x; end => nil irb(main):015:0> obj.method(:x) => #.x> In your example, it's A.class_method and not #.class_method. But it still really means the singleton class of A. I guess it's all in the service of allowing for that special case singleton-of-Class-object thing. >> I'd say that's a side-effect of the fact that A's singleton >> methods can be got at via B at all. > > yes. put another way, and something i was be-moaning several years ago, is > this: > > harp:~ > cat a.rb > class A > SINGLETON_CLASS = class << self > self > end > end > class B < A > SINGLETON_CLASS = class << self > self > end > end > > p B::SINGLETON_CLASS.ancestors > > harp:~ > ruby a.rb > [Class, Module, Object, Kernel] > > singleton classes should respect inheritence. I'm not sure what you mean here. Is it that ancestors doesn't show singleton classes? 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