# Inconsistent block parameters handling by instance\_eval

**URL:** <https://www.ruby-forum.com/t/inconsistent-block-parameters-handling-by-instance-eval/145415>\
**Category:** Ruby\
**Created:** [August 8, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/inconsistent-block-parameters-handling-by-instance-eval/145415 "2008-08-08T22:31:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 8, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/inconsistent-block-parameters-handling-by-instance-eval/145415/1 "2008-08-08T22:31:31Z")

</div>

class A  
def A.method &block  
A.new.instance\_eval &block  
end

def A.method2 &block  
A.new.instance\_eval {block.call}  
end  
end

A.method{p self} #prints #\<A:0x28c2858\>

A.method2{p self} #prints main

From this example: is instance\_eval even working in method2? It clearly  
shows that block is not called in A’s context.  
-Patrick

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 8, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/inconsistent-block-parameters-handling-by-instance-eval/145415/2 "2008-08-08T22:40:42Z")

</div>

On Friday 08 August 2008, Patrick Li wrote:

> A.method{p self} #prints #\<A:0x28c2858\>
> 
> A.method2{p self} #prints main
> 
> From this example: is instance\_eval even working in method2? It clearly  
> shows that block is not called in A’s context.  
> -Patrick

The effect of instance\_eval is to set the variable “self” to the  
receiver for  
the duration of the block. In the first case, everything works as  
expected. In  
the second case, instead, self is changed only for the outer block,  
while in  
the inner block “self” remains what it were when the block was defined.  
There’s nothing new here: the environment in which the block is executed  
doesn’t affect its variables and the fact that instance\_eval treats the  
outer  
block in a special way doesn’t otherwise change things.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 8, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/inconsistent-block-parameters-handling-by-instance-eval/145415/3 "2008-08-08T23:02:57Z")

</div>

hmmm… that makes sense.

It seems a bit counter-intuitive though. … but i guess it would have to  
be this way, otherwise closures won’t work.  
Thanks for the explanation Stefano.  
-Patrick
