#singleton_methods for objects with special singleton_class returns an empty array
def nil.bla 42 end # works nil.bla #=> 42 nil.singleton_method(:bla) #=> #<Method: NilClass#bla> NilClass.instance_methods.include? :bla #=> true # does not work nil.singleton_methods #=> 
Updated by matz (Yukihiro Matsumoto) 10 months ago
nil does not have its singleton class, but
NilClass plays the role of the singleton class (since
nil is the only instance of
So a singleton method definition defines a method in
NilClass. It's a kind of special treatment of singleton methods. Same for
We have several options:
- keep the behavior as it is, since
nildoes not have a singleton class
nil.singleton_methodsshould return methods defined in
Both have their own trade-offs. Either is OK for me but I slightly prefer the former, since it's easier.
Updated by sawa (Tsuyoshi Sawada) 10 months ago
matz (Yukihiro Matsumoto) wrote in #note-1:
nildoes not have its singleton class, but
NilClassplays the role of the singleton class
I would appreciate it if @matz (Yukihiro Matsumoto) could kindly explain what the difference is between "being a singleton class" and "playing the role of a singleton class".
At the moment, I cannot understand at all how they are different. I am actually puzzled by a related comment by matz: https://bugs.ruby-lang.org/issues/12084#note-6. Why can we not simply say "
NilClass is the singleton class of