# Passing args from within method\_missing

**URL:** <https://www.ruby-forum.com/t/passing-args-from-within-method-missing/106716>\
**Category:** Ruby\
**Created:** [June 13, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/passing-args-from-within-method-missing/106716 "2007-06-13T15:36:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sharon\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Sharon\_P](https://www.ruby-forum.com/u/Sharon_P)\
**Post date:** [June 13, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/passing-args-from-within-method-missing/106716/1 "2007-06-13T15:36:11Z")

</div>

Hi,

I have an hash of code blocks(procs?), @styles, and am trying to call  
these using method missing. Problem is, some take one argument, and  
others take two, and I can’t seem to pass args to them without if  
being passed as an array

Here’s the method\_missing code

def method\_missing style\_type, \*args  
case args.size  
when 1 : @styles [style\_type].call(args[0])  
when 2 : @styles [style\_type].call(args[0], args[1])  
end  
end

I’m sure there’s a way for me to get rid of the case statement, but  
if I try  
@styles [style\_type].call(args) it just gets sent an array.

Question is, how do I send the contents of the args array?

I realise I should probably have a means of dealing with a genuine  
method\_missing. What’s the ‘ruby way’ of doing this? if I call  
method\_missing again, wont I end in a recursive mess?

Is this making any sense?

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![Sharon\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Sharon\_P](https://www.ruby-forum.com/u/Sharon_P)\
**Post date:** [June 13, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/passing-args-from-within-method-missing/106716/2 "2007-06-13T15:52:59Z")

</div>

Sharon P. wrote:

> @styles [style\_type].call(args) it just gets sent an array.

Isn’t “@styles [style\_type].call(\*args)” what you are looking for?

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![Sharon\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Sharon\_P](https://www.ruby-forum.com/u/Sharon_P)\
**Post date:** [June 13, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/passing-args-from-within-method-missing/106716/3 "2007-06-13T16:15:18Z")

</div>

> def method\_missing style\_type, \*args  
> @styles[style\_type].call(\*args)  
> end
> 
> I hope this helps

Certainly does. I knew it would be something simple, just couldn’t  
figure it out and my Pickaxe is at work…

Thanks heaps Stefano and Wolfgang

---

<div class="post-metadata">

**Author:** ![Sharon\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Sharon\_P](https://www.ruby-forum.com/u/Sharon_P)\
**Post date:** [June 13, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/passing-args-from-within-method-missing/106716/4 "2007-06-13T15:52:13Z")

</div>

Alle mercoledì 13 giugno 2007, Sharon P. ha scritto:

> ```
> case args.size
> 
> ```
> 
> Question is, how do I send the contents of the args array?
> 
> I realise I should probably have a means of dealing with a genuine  
> method\_missing. What’s the ‘ruby way’ of doing this? if I call  
> method\_missing again, wont I end in a recursive mess?
> 
> Is this making any sense?
> 
> Cheers,  
> Dave

def method\_missing style\_type, \*args  
@styles[style\_type].call(\*args)  
end

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Sharon\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Sharon\_P](https://www.ruby-forum.com/u/Sharon_P)\
**Post date:** [June 13, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/passing-args-from-within-method-missing/106716/5 "2007-06-13T19:07:52Z")

</div>

On 6/13/07, Sharon P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m sure there’s a way for me to get rid of the case statement, but  
> if I try  
> @styles [style\_type].call(args) it just gets sent an array.
> 
> Question is, how do I send the contents of the args array?
> 
> I realise I should probably have a means of dealing with a genuine  
> method\_missing. What’s the ‘ruby way’ of doing this? if I call  
> method\_missing again, wont I end in a recursive mess?

def method\_missing(symbol, \*args)  
if prc = @styles[symbol]  
prc.call(\*args)  
else  
super  
end  
end

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

IPMS/USA Region 12 Coordinator  
[http://ipmsr12.denhaven2.com/](http://ipmsr12.denhaven2.com/)

Visit the Project Mercury Wiki Site  
[http://www.mercuryspacecraft.com/](http://www.mercuryspacecraft.com/)
