I was checking to see where methods are defined in Ruby when the definition is performed at the top level and the result is surprising:
def foo; end
singleton_class != Object # => true
self.class == Object # => true
m1 = singleton_class.instance_method :foo
# => #<UnboundMethod: Object#foo>
m2 = Object.instance_method :foo
# => #<UnboundMethod: Object#foo>
m1 == m2 # => true
It seems foo
is defined in two classes at the same time! Any explanations?