SVV SVV - 1 year ago 59
Ruby Question

What is a "main" in ruby?

I have a following lines in my ruby script. It gives the output as

when i run this script. But i cannot access that
. I don't know what is this?

Is an object or instance?

For example :

puts self #=> main

If i access the "main" means it gives the error like "undefined local
variable or method".

Previously i use this self to print its current object within a class.
For example:

class Ex
puts self #=> Ex

Here, i can use that self object to get the current class name. After
this i can able to access that
class. But in first example it
. But i can't access that. So what is a reason
behind this?

Answer Source

Short answer:

Main is the special object, with special properties.

More information:

You touched theme called Introspection. What information wikipedia gives about it:

In computing, type introspection is the ability of a program to examine the type or properties of an object at runtime.

So what you are doing is calling introspection.

So lets move further, what we can do with this?

puts self.class

This will give Object. So next important thing is:

Everything in Ruby occurs in the context of some object.

So there is nice answer which disclose this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download