What is “main” in Ruby?

Everything in Ruby occurs in the context of some object. The object at the top level is called “main”. It’s basically an instance of Object with the special property that any methods defined there are added as instance methods of Object (so they’re available everywhere).

So we can make a script consisting entirely of:

puts object_id
@a="Look, I have instance variables!"
puts @a

and it will print “105640” and “Look, I have instance variables!”.

It’s not something you generally need to concern yourself with, but it is there.

Leave a Comment