I would start with a below example :
class Foo
def self.bar
puts 12
end
end
Foo.singleton_methods # => [:bar]
Foo.ancestors # => [Foo, Object, Kernel, BasicObject]
In Ruby class(s) are also objects. Now here #bar
is a singleton method of Foo
. When you did Foo.bar
, Foo
first searched in its singleton class, then up the ancestor chains singleton class. singleton class are kind of ghost class, thus it is not visible in the ancestors chain, but it is there. It is hidden from the output of Foo.ancestors
intentionally by the language designer. singleton methods are called by the object(here FOO) directly, on which singleton(per object basis class) class has been created. You can't create an instance of singleton class.
Here is a proof :
Foo.singleton_class.new # can't create instance of singleton class (TypeError)
Do remember also that singleton methods of a class say here Bar
, is also available to its descendant class(here Foo
). Because meta class of Bar became a superclass of the meta class of Foo
, when you write class Foo < Bar..
.
class Bar
def self.biz
puts 11
end
end
class Foo < Bar
def self.bar
puts 12
end
end
Bar.singleton_class # => #<Class:Bar>
Foo.singleton_class.superclass # => #<Class:Bar>