# Redundancy in method\_missing

**URL:** <https://www.ruby-forum.com/t/redundancy-in-method-missing/149630>\
**Category:** Rails\
**Created:** [September 27, 2008, 12:53am UTC](https://www.ruby-forum.com/t/redundancy-in-method-missing/149630 "2008-09-27T00:53:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [September 27, 2008, 12:53am UTC](https://www.ruby-forum.com/t/redundancy-in-method-missing/149630/1 "2008-09-27T00:53:56Z")

</div>

Looking through the rails core code, I see method\_missing performed like  
this a lot:

def method\_missing(method, \*args)  
if block\_given?  
current\_status.send(method, \*args) { |\*block\_args|  
yield(\*block\_args) }  
else  
current\_status.send(method, \*args)  
end  
end

Why not just write:

def method\_missing(method, \*args, &block)  
current\_status.send(method, \*args, &block)  
end

It the above implementation much faster or do they perform differently?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [September 27, 2008, 11:30am UTC](https://www.ruby-forum.com/t/redundancy-in-method-missing/149630/2 "2008-09-27T11:30:31Z")

</div>

On Sep 26, 11:53 pm, Aryk G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end
> 
> Why not just write:
> 
> def method\_missing(method, \*args, &block)  
> current\_status.send(method, \*args, &block)  
> end

Building the proc object (as implied by the use of &block) is more  
expensive than just passing a block:

require ‘benchmark’  
include Benchmark

def dummy()  
yield  
end  
def dummy\_proc(&block)  
block.call  
end

bm do |x|  
x.report(‘with yield’){ 100000.times { dummy {y=1}}}  
x.report(‘with &block’) { 100000.times { dummy\_proc {y=1}}}  
end

outputs (on a fairly elderly machine):

```
  user system total real

```

with yield 0.420000 0.000000 0.420000 ( 0.464097)  
with &block 2.340000 0.040000 2.380000 ( 2.584321)

Fred

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [September 30, 2008, 1:07am UTC](https://www.ruby-forum.com/t/redundancy-in-method-missing/149630/3 "2008-09-30T01:07:49Z")

</div>

Fred, thanks for that, I still can’t believe that block.call is slower  
than yield.

All this time, I thought I was speeding thing up by passing in the  
&block.

thanks a bunch!

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [September 30, 2008, 9:08am UTC](https://www.ruby-forum.com/t/redundancy-in-method-missing/149630/4 "2008-09-30T09:08:59Z")

</div>

Sent from my iPhone

On 30 Sep 2008, at 00:07, Aryk G. \<rails-mailing-list@andreas-  
[s.net](http://s.net)\> wrote:

> Fred, thanks for that, I still can’t believe that block.call is slower  
> than yield.

If my memory is correct, it’s not the block call (try removing the  
yield/block.call from the benchmark) that is slow, but the coercing of  
the block into a proc

Fred
