# Should I use instance\_eval or block.call

**URL:** <https://www.ruby-forum.com/t/should-i-use-instance-eval-or-block-call/151500>\
**Category:** Ruby\
**Created:** [October 22, 2008, 7:49am UTC](https://www.ruby-forum.com/t/should-i-use-instance-eval-or-block-call/151500 "2008-10-22T07:49:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [October 22, 2008, 7:49am UTC](https://www.ruby-forum.com/t/should-i-use-instance-eval-or-block-call/151500/1 "2008-10-22T07:49:15Z")

</div>

I am writing a library that will be used by others. Have been reading a  
lot about procs/blocks etc, and the difference between block.call and  
instance\_eval pointed out in some articles was the context they are  
executed in.

So if I put instance\_eval in my method, the user of my lib can call  
methods in a block without using an instance. Otherwise, he has to use  
the instance to call a method.  
That means that the user has to know whether I have used block.call in  
my source or instance\_eval.

Other than that, how do i decide which to use ? Are there any other pros  
and cons?

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [October 22, 2008, 9:10am UTC](https://www.ruby-forum.com/t/should-i-use-instance-eval-or-block-call/151500/2 "2008-10-22T09:10:03Z")

</div>

2008/10/22 Nit K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am writing a library that will be used by others. Have been reading a  
> lot about procs/blocks etc, and the difference between block.call and  
> instance\_eval pointed out in some articles was the context they are  
> executed in.
> 
> So if I put instance\_eval in my method, the user of my lib can call  
> methods in a block without using an instance.

This is also true for block.call - it’s just another instance. 🙂

> Otherwise, he has to use  
> the instance to call a method.  
> That means that the user has to know whether I have used block.call in  
> my source or instance\_eval.
> 
> Other than that, how do i decide which to use ? Are there any other pros  
> and cons?

It seems there is a tendency to use block call and yield self because  
it is more obvious. Also, in case of attribute setters using  
instance\_eval does not really help because of the local variable  
method ambiguity:

irb(main):001:0\> Foo = Struct.new :bar do def test(&b) instance\_eval(&b)  
end end  
=\> Foo  
irb(main):002:0\> f=Foo.new  
=\> #  
irb(main):003:0\> f.test { bar = 10 }  
=\> 10  
irb(main):004:0\> f  
=\> #  
irb(main):005:0\> f.test { self.bar = 10 }  
=\> 10  
irb(main):006:0\> f  
=\> #  
irb(main):007:0\>

Having said that, the situation for DSL’s is different: here it is  
often desired to not have to use an instance for cleaner syntax and  
you want to provide some context. In those cases it’s probably better  
to use instance\_eval.

Kind regards

robert
