0

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?

MMSequeira
  • 61
  • 6

1 Answers1

0

The question makes no sense, really. The method is defined just in Object. One may get to it through the singleton class, of course, but it is not defined there:

singleton_class.instance_methods(false).grep /foo/  # => []

Sigh...

MMSequeira
  • 61
  • 6