[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Passing args from within method_missing

David and Sharon Phillips

6/13/2007 1:36:00 PM

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

4 Answers

Stefano Crocco

6/13/2007 1:51:00 PM

0

Alle mercoledì 13 giugno 2007, Sharon Phillips ha scritto:
> 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

def method_missing style_type, *args
@styles[style_type].call(*args)
end

I hope this helps

Stefano

WoNáDo

6/13/2007 1:53:00 PM

0

Sharon Phillips 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

--
Posted via http://www.ruby-....

David and Sharon Phillips

6/13/2007 2:13:00 PM

0

> 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

Rick DeNatale

6/13/2007 5:07:00 PM

0

On 6/13/07, Sharon Phillips <phillipsds@yahoo.co.uk> wrote:


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

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


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...