# Instance\_eva()l-ing a proc with parameters

**URL:** <https://www.ruby-forum.com/t/instance-eva-l-ing-a-proc-with-parameters/70757>\
**Category:** Ruby\
**Created:** [October 2, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/instance-eva-l-ing-a-proc-with-parameters/70757 "2006-10-02T18:37:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [October 2, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/instance-eva-l-ing-a-proc-with-parameters/70757/1 "2006-10-02T18:37:27Z")

</div>

Dear friends,

I need to evaluate a proc within an instance context and would like to  
pass it a parameter. The only straight forward way I found is to use  
instance\_eval(), however it does not allow to pass parameters to a proc,  
only the instance itself is passed as a single parameter:

[rh-30-x86.irvspxl08:1004]gbystrit\> irb  
irb(main):001:0\> [VERSION, PLATFORM]  
=\> [“1.8.4”, “i686-linux”]  
irb(main):002:0\> block = proc { |\*\_args| puts \_args.inspect }  
=\> #Proc:0xb75b21fc@:2(irb)  
irb(main):003:0\> class A; end  
=\> nil  
irb(main):004:0\> a = A.new  
=\> #\<A:0xb75a9674\>  
irb(main):005:0\> a.instance\_eval &block  
[#\<A:0xb75a9674\>]  
=\> nil  
irb(main):006:0\> a.instance\_eval 1, 2, &block # Hypothetical  
ArgumentError: wrong number of arguments (2 for 0)  
from (irb):6:in `instance\_eval’  
from (irb):6  
from :0

Is there any easy way to do what I want? I also found that I can use  
define\_method() to turn a proc into an instance method and then call it  
with parameters, however it involves a lot of hassle (for one,  
define\_method() is private).

Any thoughts?

Thank you,  
Gennady B…

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [October 2, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/instance-eva-l-ing-a-proc-with-parameters/70757/2 "2006-10-02T18:51:28Z")

</div>

On Tue, Oct 03, 2006 at 01:35:48AM +0900, Gennady B. wrote:

> Dear friends,
> 
> I need to evaluate a proc within an instance context and would like to  
> pass it a parameter. The only straight forward way I found is to use  
> instance\_eval(), however it does not allow to pass parameters to a proc,  
> only the instance itself is passed as a single parameter:  
> Google for instance\_exec, there are various and sundry implementations,  
> including one built into rails.

[eigenclass.org](http://eigenclass.org/hiki.rb?instance_exec) has various implementations  
and their respective strengths and weaknesses. Or you can just use ruby  
1.9 where it’s part of the core.
