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

**URL:** <https://www.ruby-forum.com/t/how-to-change-the-owner-of-a-method-in-a-block/148201>\
**Category:** Ruby\
**Created:** [September 10, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/how-to-change-the-owner-of-a-method-in-a-block/148201 "2008-09-10T12:28:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 10, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/how-to-change-the-owner-of-a-method-in-a-block/148201/1 "2008-09-10T12:28:21Z")

</div>

Hi, let me explain with this simple code:

* * *

class A

def initialize(&blk)  
blk.call  
end

def hello  
puts “HELLO !!!”  
end

end

a=A.new { hello() }

## =\> block\_1.rb:13: undefined method `hello' for main:Object (NoMethodError) from block_1.rb:4:in`call’ from block\_1.rb:4:in `initialize' from block_1.rb:13:in`new’ 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.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 10, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/how-to-change-the-owner-of-a-method-in-a-block/148201/2 "2008-09-10T12:40:10Z")

</div>

one way would be to use instance\_eval :

def new(&blk)  
instance\_eval &blk  
end

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 10, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/how-to-change-the-owner-of-a-method-in-a-block/148201/3 "2008-09-10T12:40:57Z")

</div>

Lex W. wrote:

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

I meant to write :

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

too much java can get you high .

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 10, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/how-to-change-the-owner-of-a-method-in-a-block/148201/4 "2008-09-10T13:40:33Z")

</div>

2008/9/10, Lex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> instance_eval &blk
> 
> ```
> 
> 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.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 10, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/how-to-change-the-owner-of-a-method-in-a-block/148201/5 "2008-09-10T14:08:46Z")

</div>

From: IÃ±aki Baz C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# \>\> 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

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 10, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/how-to-change-the-owner-of-a-method-in-a-block/148201/6 "2008-09-10T14:32:55Z")

</div>

2008/9/10, PeÃ±a, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> =\> 2.288818359375e-05  
> You are right!  
> So using “instance\_eval” is ~ 40% slower than running directly the  
> code.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 11, 2008, 2:50am UTC](https://www.ruby-forum.com/t/how-to-change-the-owner-of-a-method-in-a-block/148201/7 "2008-09-11T02:50:37Z")

</div>

On Wed, Sep 10, 2008 at 7:24 PM, Iñaki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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|  
x.report(“normal”) { n.times { a=nil;a=123; a2=a_2; a3=a_3 }}  
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};  
instance\_eval{ a2=a_2};  
instance\_eval{a3=a_3}  
}  
}  
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

```
               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

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [September 11, 2008, 4:14am UTC](https://www.ruby-forum.com/t/how-to-change-the-owner-of-a-method-in-a-block/148201/8 "2008-09-11T04:14:18Z")

</div>

El Jueves, 11 de Septiembre de 2008, botp  
escribió:\> x.report(“string”) { n.times {instance\_eval ‘a=nil;a=123; a2=a_2; a3=a_3’

> 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

Good comparision, thanks 🙂
