# Mixing instance\_eval and block with arguments

**URL:** <https://www.ruby-forum.com/t/mixing-instance-eval-and-block-with-arguments/176946>\
**Category:** Ruby\
**Created:** [October 8, 2009, 6:04pm UTC](https://www.ruby-forum.com/t/mixing-instance-eval-and-block-with-arguments/176946 "2009-10-08T18:04:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [October 8, 2009, 6:04pm UTC](https://www.ruby-forum.com/t/mixing-instance-eval-and-block-with-arguments/176946/1 "2009-10-08T18:04:51Z")

</div>

Hi,

here’s what I’d like to do in a silly example:

S = Struct.new :foo, :bar

init = lambda do |a, b|  
self.foo = b  
self.bar = a + 10  
end

s = S.new # or create an instance otherwise

# now comes the fun part which does not work

s.instance\_eval(1, 2, &init)  
=\> #

In other words: I like to define a block as an initializer which I can  
store away somewhere and invoke that initializer later on in the  
context of “self” and pass arguments at the same time. Any ideas how  
I can accomplish this elegantly?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [October 8, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/mixing-instance-eval-and-block-with-arguments/176946/2 "2009-10-08T18:53:27Z")

</div>

On Thu, Oct 8, 2009 at 6:04 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> s = S.new # or create an instance otherwise
> 
> # now comes the fun part which does not work
> 
> s.instance\_eval(1, 2, &init)  
> =\> #
> 
> In other words: I like to define a block as an initializer which I can  
> store away somewhere and invoke that initializer later on in the  
> context of “self” and pass arguments at the same time. Any ideas how  
> I can accomplish this elegantly?

Probably not exactly what you want (can’t make self to not be main  
inside the lambda), but what about:

irb(main):001:0\> S = Struct.new :foo, :bar  
=\> S  
irb(main):003:0\> s = S.new  
=\> #  
irb(main):040:0\> def s.init(\*args,&blk) #you can put this in a module  
and extend s with it, or add it to S  
irb(main):041:1\> blk[self,_args]  
irb(main):042:1\> end  
irb(main):043:0\> init = lambda do |o,a,b|  
irb(main):044:1_ o.foo = b  
irb(main):045:1\> o.bar = a + 10  
irb(main):046:1\> end  
=\> #Proc:0xb7d78f18@:43(irb)  
irb(main):047:0\> s.init(1,2,&init)  
=\> 11  
irb(main):048:0\> s  
=\> #

Don’t know if this is good enough.

Jesus.

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [October 8, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/mixing-instance-eval-and-block-with-arguments/176946/3 "2009-10-08T21:22:10Z")

</div>

On Thu, Oct 8, 2009 at 10:04 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> In other words: I like to define a block as an initializer which I can  
> store away somewhere and invoke that initializer later on in the  
> context of “self” and pass arguments at the same time. Any ideas how  
> I can accomplish this elegantly?

Sounds like you need instance\_exec, which is a 1.9 feature but there are  
implementations for 1.8:

[http://eigenclass.org/hiki.rb?instance\_exec](http://eigenclass.org/hiki.rb?instance_exec)

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [October 8, 2009, 10:41pm UTC](https://www.ruby-forum.com/t/mixing-instance-eval-and-block-with-arguments/176946/4 "2009-10-08T22:41:48Z")

</div>

On 10/08/2009 06:52 PM, Jesús Gabriel y Galán wrote:

> > self.bar = a + 10  
> > context of “self” and pass arguments at the same time. Any ideas how  
> > and extend s with it, or add it to S  
> > =\> #
> 
> Don’t know if this is good enough.

Thank you! Unfortunately I tried to avoid explicit passing of the  
reference.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [October 8, 2009, 10:42pm UTC](https://www.ruby-forum.com/t/mixing-instance-eval-and-block-with-arguments/176946/5 "2009-10-08T22:42:23Z")

</div>

On 10/08/2009 09:21 PM, Tony A. wrote:

> Sounds like you need instance\_exec, which is a 1.9 feature but there are  
> implementations for 1.8:
> 
> [http://eigenclass.org/hiki.rb?instance\_exec](http://eigenclass.org/hiki.rb?instance_exec)

1.9 is OK.

irb(main):001:0\> l = lambda {|a| self + a}  
=\> #\<Proc:0x9f5cfe0@(irb):1 (lambda)\>  
irb(main):002:0\> 1.instance\_exec(2,&l)  
=\> 3

Perfect! This is _exactly_ what I need. Thanks a bunch!

This article is so old, I can’t believe I missed this completely.  
Amazing: learn something new every day.

Kind regards

robert
