1

What I know singleton methods can be called by the objects, on which it is defined. Now in the below example C is also an object of Class and singleton method a_class_method defined on the Class object C. So how does another Class object D able to call a_class_method?

How does object individuation principle holds in this example?

class C
end
#=> nil

def C.a_class_method
 puts "Singleton method defined on #{self}"
end
#=> nil

C.a_class_method
#Singleton method defined on C
#=> nil

class D < C
end
#=> nil

D.a_class_method
#Singleton method defined on D
#=> nil
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317

2 Answers2

1

well when you did the < you made class D inherit from Class C so D will get anything from class C. If you want to know what all D's parents are you can do

puts "D's parent Classes = #{D.ancestors.join(',')}"

which will give you

D's parent Classes = D,C,Object,Kernel

So even though D would be an individual class it is a sub class of C which is what lets it use a method defined for C

fullobs
  • 53
  • 1
  • 5
  • My point was regarding the `individuation` . What you said is correct interns of `inheritance`. Which is understood. But we generally called `singleton` methods by the object with which we have created. Now here it is the singleton method created on the Class object `C`, But we are getting access to that method via the class object `D` too. -- This is the confusion I am having. – Arup Rakshit Mar 16 '13 at 05:53
  • Ok, I did not even know you could do that, i though singleton methods where only really for objects not for classes. Hopefully someone with a better grasp of this will chime in. – fullobs Mar 16 '13 at 13:35
  • May I have some vote for this, which might increase this post's interest? :) – Arup Rakshit Mar 16 '13 at 14:35
  • just opened up the account yesterday so I cannot up vote yet :/ need 2 more reputation apparently – fullobs Mar 16 '13 at 15:38
  • K thanks, I am curious to hear what response you get. I did some searches because I was curious and all the singleton stuff I found had to do with defining something on an object not on a class ex: `objectC=C.new` `def objectC.singleton_method` `something` `end` D.singleton_method will not exist then and neither will C.singleton_method because that was defined only for the singleton class for the objectC – fullobs Mar 16 '13 at 16:36
  • Humm you can taken a look another post of [mine](http://stackoverflow.com/questions/15451396/where-does-singleton-methods-reside-in-ruby?noredirect=1#comment21861842_15451396). Hope you would enjoy it. – Arup Rakshit Mar 16 '13 at 16:38
  • I see you understand that by your other post :) – fullobs Mar 16 '13 at 16:42
1

The reason that a_class_method is available is that:

D.singleton_class.superclass == C.singleton_class
 # => true
Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
  • here is another [post](http://stackoverflow.com/questions/15453509/confusion-with-instance-variables-object-id-allocation-in-ruby) - hope you will enjoy it. :) – Arup Rakshit Mar 16 '13 at 19:12