From: 7stud -- Date: 2011-08-23T12:51:45+09:00 Subject: Re: Gritty Details of super() I wondered if the singleton class of a singleton class inherits from the singleton class of a parent's singleton class: class Cat class << self #Cat's singleton class class << self #singleton class of Cat's singleton class def speak puts 'meow' end end end end class Dog < Cat class << self #Dog's singleton class def greet puts 'hello' end end end Dog.greet Dog.singleton_class.speak --output:-- hello meow So the lookup diagram looks something like this: Object -> #Object ##Object ^ ^ ^ | | | Cat --> #Cat ##Cat ^ ^ ^ | | | Dog --> #Dog ##Dog ^ | d => #d Now, we know that every class object inherits from Class. That means any instance methods defined in class can be called by a class object, e.g. new(). Therefore, #Object should be able to call instance methods defined in Class: Class ^ | Object -> #Object ##Object ^ ^ ^ | | | Cat --> #Cat ##Cat ^ ^ ^ | | | Dog --> #Dog ##Dog ^ | d => #d But ##Object is also a class object, so it should be able to call instance methods defined in Class as well: Class Class ^ ^ | | Object -> #Object ##Object ^ ^ ^ | | | Cat --> #Cat ##Cat ^ ^ ^ | | | Dog --> #Dog ##Dog ^ | d => #d Let's test that: class Class def eat puts "chomp, chomp" end end class Cat class << self #Cat's singleton class class << self #singleton class of Cat's singleton class def speak puts 'meow' end end end end class Dog < Cat class << self #Dog's singleton class def greet puts 'hello' end end end Dog.greet #=>hello Dog.singleton_class.speak #=>meow Dog.singleton_class.eat #=>chomp, chomp Dog.singleton_class.singleton_class.eat #=>chomp, chomp -- Posted via http://www.ruby-forum.com/.