class Hi
self #=> Hi
class << self #same as 'class << Hi'
self #=> #<Class:Hi>
self == Hi.singleton_class #=> true
end
end
[it makes self == thing.singleton_class
in the context of its block].
hi = String.new
def hi.a
end
hi.class.instance_methods.include? :a #=> false
hi.singleton_class.instance_methods.include? :a #=> true
hi
object inherits its #methods
from its #singleton_class.instance_methods
and then from its #class.instance_methods
.
Here we gave hi
's singleton class instance method :a
. It could have been done with class << hi instead.
hi
's #singleton_class
has all instance methods hi
's #class
has, and possibly some more (:a
here).
[instance methods of thing's #class
and #singleton_class
can be applied directly to thing. when ruby sees thing.a, it first looks for :a method definition in thing.singleton_class.instance_methods and then in thing.class.instance_methods]
By the way - they call object's singleton class == metaclass == eigenclass.