In ruby, when one defines a method in the root scope, it can be called from that scope:
def foo
"foo"
end
foo #=> "foo"
In any other context this is not the case:
class Bar
def foo
"foo"
end
foo #=> Error: No Method `foo` for class Bar
end
What mechanism is used in setting up the main object (an instance of Object) that allows this to happen?
This is really special cased in Ruby. If you define methods in the global scope they get actually defined on
Kernelwhich is included in every object by default.Kernel is also there when no other context is defined. Since Class inherits also from Kernel methods defined on it are also in scope in class scopes.