# How to do instance\_eval with arguments

**URL:** <https://www.ruby-forum.com/t/how-to-do-instance-eval-with-arguments/64808>\
**Category:** Ruby\
**Created:** [February 8, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/how-to-do-instance-eval-with-arguments/64808 "2006-02-08T23:33:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimweirich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jimweirich](https://www.ruby-forum.com/u/jimweirich)\
**Post date:** [February 8, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/how-to-do-instance-eval-with-arguments/64808/1 "2006-02-08T23:33:38Z")

</div>

I’m prototyping a DSL and came across a situtation where I have a lambda  
that I would like to give to instance\_eval, but the lambda takes  
arguments. Instance\_eval will not supply any arguments when evaluating  
the lambda and I don’t see a straightforward way around this.

Any suggestions?

Here’s a test case …

class Dummy  
def f  
:dummy\_value  
end  
end

def instance\_eval\_with\_args(obj, \*args, &block)

# Magic goes here to evaluate +block+ in the scope of

# +obj+, yet pass a list of argument values to the block.

end

class TestInstanceEvalWithArgs \< Test::Unit::TestCase  
def test\_instance\_eval\_with\_args  
# Create a block that returns the value of an argument and a value  
# of a method call to +self+. This is the basic functionality I  
need.  
block = lambda { |a| [a, f] }

```
assert_equal [:arg_value, :dummy_value],
  instance_eval_with_args(Dummy.new, :arg_value, &block)

```

end  
end

–  
– Jim W.

---

<div class="post-metadata">

**Author:** ![jimweirich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jimweirich](https://www.ruby-forum.com/u/jimweirich)\
**Post date:** [February 8, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/how-to-do-instance-eval-with-arguments/64808/2 "2006-02-08T23:58:07Z")

</div>

On Thu, Feb 09, 2006 at 07:33:53AM +0900, Jim W. wrote:

> I’m prototyping a DSL and came across a situtation where I have a lambda  
> that I would like to give to instance\_eval, but the lambda takes  
> arguments. Instance\_eval will not supply any arguments when evaluating  
> the lambda and I don’t see a straightforward way around this.
> 
> Any suggestions?

This is what 1.9’s Object#instance\_exec does:  
[eigenclass.org](http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l10)

One way to implement it in 1.8:

# The following script was annotated with my xmp filter

# 

[http://eigenclass.org/hiki.rb?Enhanced+xmp+code+evaluation+and+annotation](http://eigenclass.org/hiki.rb?Enhanced+xmp+code+evaluation+and+annotation)

# 

class Object  
def instance\_exec(\*args, &block)  
mname = “\__instance\_exec_#{Thread.current.object\_id.abs}”  
class \<\< self; self end.class\_eval{ define\_method(mname, &block) }  
begin  
ret = send(mname, \*args)  
ensure  
class \<\< self; self end.class\_eval{ undef\_method(mname) } rescue  
nil  
end  
ret  
end  
end

# your test case

class Dummy  
def f  
:dummy\_value  
end  
end

def instance\_eval\_with\_args(obj, \*args, &block)

# Magic goes here to evaluate +block+ in the scope of

# +obj+, yet pass a list of argument values to the block.

obj.instance\_exec(\*args, &block)  
end

require ‘test/unit’  
class TestInstanceEvalWithArgs \< Test::Unit::TestCase  
def test\_instance\_eval\_with\_args  
# Create a block that returns the value of an argument and a value  
# of a method call to +self+. This is the basic functionality I  
need.  
block = lambda { |a| [a, f] }

```
assert_equal [:arg_value, :dummy_value],
  instance_eval_with_args(Dummy.new, :arg_value, &block)

```

end  
end  
**END**

# \>\> Loaded suite -

# \>\> Started

# \>\> .

# \>\> Finished in 0.000561 seconds.

# \>\>

# \>\> 1 tests, 1 assertions, 0 failures, 0 errors

---

<div class="post-metadata">

**Author:** ![jimweirich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jimweirich](https://www.ruby-forum.com/u/jimweirich)\
**Post date:** [February 9, 2006, 12:00am UTC](https://www.ruby-forum.com/t/how-to-do-instance-eval-with-arguments/64808/3 "2006-02-09T00:00:57Z")

</div>

Hi,

In message “Re: how to do instance\_eval with arguments”  
on Thu, 9 Feb 2006 07:33:53 +0900, Jim W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:  
|  
|I’m prototyping a DSL and came across a situtation where I have a lambda  
|that I would like to give to instance\_eval, but the lambda takes  
|arguments. Instance\_eval will not supply any arguments when evaluating  
|the lambda and I don’t see a straightforward way around this.  
|  
|Any suggestions?

CVS HEAD (1.9) has instance\_exec method that works exactly what you  
want.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![jimweirich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jimweirich](https://www.ruby-forum.com/u/jimweirich)\
**Post date:** [February 9, 2006, 1:49am UTC](https://www.ruby-forum.com/t/how-to-do-instance-eval-with-arguments/64808/4 "2006-02-09T01:49:54Z")

</div>

Mauricio F. wrote:

> On Thu, Feb 09, 2006 at 07:33:53AM +0900, Jim W. wrote:
> 
> > Any suggestions?  
> > […]

> One way to implement it in 1.8:

[… solution elided …]

Thanks! That should be good enough for my prototype … at least until  
1.9 features become standard.

–  
– Jim W.

---

<div class="post-metadata">

**Author:** ![jimweirich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jimweirich](https://www.ruby-forum.com/u/jimweirich)\
**Post date:** [February 9, 2006, 1:46am UTC](https://www.ruby-forum.com/t/how-to-do-instance-eval-with-arguments/64808/5 "2006-02-09T01:46:01Z")

</div>

Yukihiro M. wrote:

> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:  
> |  
> |I’m prototyping a DSL and came across a situtation where I have a lambda  
> |that I would like to give to instance\_eval, but the lambda takes  
> |arguments. Instance\_eval will not supply any arguments when evaluating  
> |the lambda and I don’t see a straightforward way around this.  
> |  
> |Any suggestions?
> 
> CVS HEAD (1.9) has instance\_exec method that works exactly what you  
> want.

Excellent! I think Matz’s time machine is every bit as good as  
Guido’s[1].

–  
– Jim W.

[1] [Guido](http://www.catb.org/jargon/html/G/Guido.html)
