# What's the main inside the block

**URL:** <https://www.ruby-forum.com/t/whats-the-main-inside-the-block/61598>\
**Category:** Ruby\
**Created:** [May 25, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/whats-the-main-inside-the-block/61598 "2006-05-25T14:56:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [May 25, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/whats-the-main-inside-the-block/61598/1 "2006-05-25T14:56:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [May 25, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/whats-the-main-inside-the-block/61598/2 "2006-05-25T15:23:02Z")

</div>

> > > > > “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

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [May 25, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/whats-the-main-inside-the-block/61598/3 "2006-05-25T15:28:07Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![taq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@taq](https://www.ruby-forum.com/u/taq)\
**Post date:** [May 25, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/whats-the-main-inside-the-block/61598/4 "2006-05-25T15:32:47Z")

</div>

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

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

yes,

Guy Decoux
