# How to change the owner of a method in a block?

**URL:** <https://rubytalk.org/t/how-to-change-the-owner-of-a-method-in-a-block/48978>\
**Category:** ruby-talk\
**Created:** [10 September 2008 10:21 UTC](https://rubytalk.org/t/how-to-change-the-owner-of-a-method-in-a-block/48978 "2008-09-10T10:21:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [10 September 2008 10:21 UTC](https://rubytalk.org/t/how-to-change-the-owner-of-a-method-in-a-block/48978/1 "2008-09-10T10:21:20Z")

</div>

Hi, let me explain with this simple code:

> **···**
>
> --------------------  
> class A
> 
> &nbsp;&nbsp;def initialize(&blk)  
> &nbsp;&nbsp;&nbsp;&nbsp;blk.call  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def hello  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "HELLO !!!"  
> &nbsp;&nbsp;end
> 
> end
> 
> a=A.new { hello() }
> 
> =\> block\_1.rb:13: undefined method `hello' for main:Object (NoMethodError)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from block\_1.rb:4:in `call'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from block\_1.rb:4:in `initialize'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from block\_1.rb:13:in `new'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from block\_1.rb:13  
> ---------------------------------
> 
> I understand perfectly what occurs: When passing "hello()" in a block,  
> it's like if the current object is "main:Object", for which there is  
> no "hello" method (of course).
> 
> Well, I'd like that "hello()" would be runned as class A instance  
> method. Is it possible is some ellegant way?
> 
> Thanks a lot.
> 
> --  
> Iñaki Baz Castillo  
> \<[ibc@aliax.net](mailto:ibc@aliax.net)\>

---

<div class="post-metadata">

**Author:** ![Lex\_Williams](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@Lex\_Williams](https://rubytalk.org/u/Lex_Williams)\
**Post date:** [10 September 2008 10:34 UTC](https://rubytalk.org/t/how-to-change-the-owner-of-a-method-in-a-block/48978/2 "2008-09-10T10:34:28Z")

</div>

Lex Williams wrote:

> one way would be to use instance\_eval :
> 
> def new(&blk)  
> &nbsp;&nbsp;&nbsp;instance\_eval &blk  
> end

I meant to write :

def initialize(&blk)  
&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval &blk  
end

too much java can get you high .

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [10 September 2008 10:49 UTC](https://rubytalk.org/t/how-to-change-the-owner-of-a-method-in-a-block/48978/3 "2008-09-10T10:49:25Z")

</div>

> Lex Williams wrote:  
> \> one way would be to use instance\_eval :  
> \>  
> \> def new(&blk)  
> \> instance\_eval &blk  
> \> end
> 
> I meant to write :
> 
> &nbsp;&nbsp;def initialize(&blk)  
> &nbsp;&nbsp;&nbsp;&nbsp;instance\_eval &blk  
> &nbsp;&nbsp;end

Really great 🙂

Let me just a question: how does affect using "instance\_eval" in  
performance? I've done some simple tests:

> > Benchmark.realtime { a=123; a2=a\*2; a3=a\*3 }

# =\> 1.97887420654297e-05

> > Benchmark.realtime { instance\_eval 'a=123; a2=a\*2; a3=a\*3' }

# =\> 4.50611114501953e-05

Well, it seems that using "instance\_eval" takes the double of time  
than running the code directly. Is it significant?

Thanks.

> **···**
>
> 2008/9/10, Lex Williams \<etaern@yahoo.com\>:
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [10 September 2008 10:58 UTC](https://rubytalk.org/t/how-to-change-the-owner-of-a-method-in-a-block/48978/4 "2008-09-10T10:58:22Z")

</div>

# \>\> Benchmark.realtime { instance\_eval 'a=123; a2=a\*2; a3=a\*3' }  
# # =\> 4.50611114501953e-05

that's a string.

include a block, eg

> Benchmark.realtime { a=123; a2=a\*2; a3=a\*3 }

=\> 1.59740447998047e-05

> Benchmark.realtime { instance\_eval 'a=123; a2=a\*2; a3=a\*3' }

=\> 7.58171081542969e-05

> Benchmark.realtime { instance\_eval {a=123; a2=a\*2; a3=a\*3} }

=\> 2.288818359375e-05

> **···**
>
> From: Iñaki Baz Castillo [mailto:ibc@aliax.net]

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [10 September 2008 11:24 UTC](https://rubytalk.org/t/how-to-change-the-owner-of-a-method-in-a-block/48978/5 "2008-09-10T11:24:11Z")

</div>

You are right!  
So using "instance\_eval" is ~ 40% slower than running directly the code.

Thanks a lot.

> **···**
>
> 2008/9/10, Peña, Botp \<botp@delmonte-phil.com\>:
> 
> > From: Iñaki Baz Castillo [mailto:ibc@aliax.net]  
> > # \>\> Benchmark.realtime { instance\_eval 'a=123; a2=a\*2; a3=a\*3' }  
> > # # =\> 4.50611114501953e-05
> > 
> > that's a string.
> > 
> > include a block, eg
> > 
> > \> Benchmark.realtime { a=123; a2=a\*2; a3=a\*3 }
> > 
> > =\> 1.59740447998047e-05
> > 
> > \> Benchmark.realtime { instance\_eval 'a=123; a2=a\*2; a3=a\*3' }
> > 
> > =\> 7.58171081542969e-05
> > 
> > \> Benchmark.realtime { instance\_eval {a=123; a2=a\*2; a3=a\*3} }
> > 
> > =\> 2.288818359375e-05
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [11 September 2008 00:43 UTC](https://rubytalk.org/t/how-to-change-the-owner-of-a-method-in-a-block/48978/6 "2008-09-11T00:43:38Z")

</div>

well, we can try again 🙂

botp@jedi-hopeful:~$ cat test.rb  
require 'benchmark'  
n=100\_000  
Benchmark.bmbm do |x|  
&nbsp;&nbsp;x.report("normal") { n.times { a=nil;a=123; a2=a\*2; a3=a\*3 }}  
&nbsp;&nbsp;x.report("string") { n.times {instance\_eval 'a=nil;a=123; a2=a\*2; a3=a\*3' }}  
&nbsp;&nbsp;x.report("block") { n.times {instance\_eval {a=nil;a=123; a2=a\*2; a3=a\*3} }}  
&nbsp;&nbsp;x.report("block-broken") { n.times {a=nil;instance\_eval{a=123};  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval{ a2=a\*2};  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval{a3=a\*3}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
end

botp@jedi-hopeful:~$ ruby test.rb  
Rehearsal ------------------------------------------------  
normal 0.140000 0.000000 0.140000 ( 0.247775)  
string 2.420000 0.010000 2.430000 ( 3.438391)  
block 0.250000 0.000000 0.250000 ( 0.375656)  
block-broken 0.470000 0.010000 0.480000 ( 0.698093)  
--------------------------------------- total: 3.300000sec

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
normal 0.120000 0.000000 0.120000 ( 0.216789)  
string 2.480000 0.020000 2.500000 ( 3.291342)  
block 0.300000 0.000000 0.300000 ( 0.423327)  
block-broken 0.490000 0.000000 0.490000 ( 0.703046)  
botp@jedi-hopeful:~$

slower but not slow (in my own mileage 🙂  
or optimistically, direct code is faster than a code block

> **···**
>
> On Wed, Sep 10, 2008 at 7:24 PM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> 
> > So using "instance\_eval" is ~ 40% slower than running directly the code.

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [11 September 2008 02:06 UTC](https://rubytalk.org/t/how-to-change-the-owner-of-a-method-in-a-block/48978/7 "2008-09-11T02:06:51Z")

</div>

Good comparision, thanks 🙂

> **···**
>
> El Jueves, 11 de Septiembre de 2008, botp escribió:
> 
> > On Wed, Sep 10, 2008 at 7:24 PM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:  
> > \> So using "instance\_eval" is ~ 40% slower than running directly the code.
> > 
> > well, we can try again 🙂
> > 
> > botp@jedi-hopeful:~$ cat test.rb  
> > require 'benchmark'  
> > n=100\_000  
> > Benchmark.bmbm do |x|  
> > &nbsp;&nbsp;x.report("normal") { n.times { a=nil;a=123; a2=a\*2; a3=a\*3 }}  
> > &nbsp;&nbsp;x.report("string") { n.times {instance\_eval 'a=nil;a=123; a2=a\*2; a3=a\*3'  
> > }} x.report("block") { n.times {instance\_eval {a=nil;a=123; a2=a\*2; a3=a\*3}  
> > }} x.report("block-broken") { n.times {a=nil;instance\_eval{a=123};  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval{ a2=a\*2};  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval{a3=a\*3}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > end
> > 
> > botp@jedi-hopeful:~$ ruby test.rb  
> > Rehearsal ------------------------------------------------  
> > normal 0.140000 0.000000 0.140000 ( 0.247775)  
> > string 2.420000 0.010000 2.430000 ( 3.438391)  
> > block 0.250000 0.000000 0.250000 ( 0.375656)  
> > block-broken 0.470000 0.010000 0.480000 ( 0.698093)  
> > --------------------------------------- total: 3.300000sec
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> > normal 0.120000 0.000000 0.120000 ( 0.216789)  
> > string 2.480000 0.020000 2.500000 ( 3.291342)  
> > block 0.300000 0.000000 0.300000 ( 0.423327)  
> > block-broken 0.490000 0.000000 0.490000 ( 0.703046)  
> > botp@jedi-hopeful:~$
> > 
> > slower but not slow (in my own mileage 🙂  
> > or optimistically, direct code is faster than a code block
> 
> --  
> Iñaki Baz Castillo
