Maybe you’ve noticed that Ruby scripts are executed in some special space or scope which is called “main”:

inspect # => "main"

So, let's try to clarify what it is.

self.class # => Object

method(:inspect).owner # => #<Class:#<Object:0x000055bd67f9dd58>>

method(:inspect).owner == singleton_class # => true

It looks like main is an instance of Object Class instance, while a method inspect is defined as it’s Singleton:

def self.inspect

"my-main"

end inspect # => "my-main" singleton_class.instance_methods(false) # => [:inspect, :to_s]

It’s expected behavior since Ruby check’s Singleton methods in the first place and just then instance methods defined by Class instance which produced it. So, if main is an instance produced by Object then what about instance variables?

@instance_var = 1

@instance_var # => 1

Works as expected, but what about regular methods defined with def which we could define without using Class scope? Maybe it would be better do not have such ability, so every method would be possible to define only in the context of Class instance, but currently, we have such ability, so let's check it:

def my_method

'method in main scope'

end my_method # => 'method in main scope'

This is the place where main is no more just instance produced by Object Class instance, but something more, because we could add methods to already existed instance without using Singleton or Object Class instance, so it works like we are in the scope of Object Class instance and defining regular instance method:

method(:my_method).owner # => Object

Object.instance_methods.include?(:my_method) # => true Object.new.my_method # => Error: private method `my_method' called

As you could see, it’s not a regular instance method, it’s private , so methods defined in main scope are defined as a private methods of Object Class instance. One more thing which works in main scope as in Class instance scope are constants: