What's the main inside the block

Hi there.

I’m wondering what “main” inside a block means.
For example:

%w(one).each { puts self } => main

What is exactly the “main” thing there?

Btw, to make the block recognize the object instance context where it is
running (on this case, the Array created with %w) the solution is using
an instance_eval, as the instance_eval description says:

“In order to set the context, the variable self is set to obj while the
code is executing, giving the code access to obj’s instance variables.”

Like:

%w(one two three).instance_eval{ each_with_index {|v,i| puts
“#{i+1}/#{self.size}=#{v}” } }
=>
1/3=one
2/3=two
3/3=three

On this example, used for checking the size of the created object where
the block is executing.

Thanks!

“E” == =?utf-8?Q?Eust=c3=a1quio Rangel?= writes:

E> %w(one).each { puts self } => main

E> What is exactly the “main” thing there?

This is self at toplevel. It’s an instance of Object

moulon% ruby -e ‘puts self, self.class’
main
Object
moulon%

Guy Decoux

E> What is exactly the “main” thing there?
This is self at toplevel. It’s an instance of Object

Thanks for the explanation, Guy!

About the object instance context, am I right? The way to make the block
see the object that invoked it?

Thanks again.

“E” == =?utf-8?Q?Eust=c3=a1quio Rangel?= writes:

E> About the object instance context, am I right?

yes,

Guy Decoux