[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Order of arguments

Kless

8/26/2008 10:44:00 PM

if I define a function with several args. as
----------
def func(foo, bar)
----------
and is used with the changed args.:
----------
func(bar='wrong', foo='order')*
----------
it will change the order of args. and this is not desirable, how to
solve it?

I imagine this with a lot of args. and could be danger
3 Answers

Joel VanderWerf

8/26/2008 10:49:00 PM

0

Kless wrote:
> if I define a function with several args. as
> ----------
> def func(foo, bar)
> ----------
> and is used with the changed args.:
> ----------
> func(bar='wrong', foo='order')*
> ----------
> it will change the order of args. and this is not desirable, how to
> solve it?
>
> I imagine this with a lot of args. and could be danger

Use a hash argument

def func(args={})
bar=args[:bar]
foo=args[:foo]
end

func(:bar=>1, :foo=>2)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Phlip

8/27/2008 12:48:00 AM

0

Kless wrote:

> func(bar='wrong', foo='order')

Supplementing Joel's answer - that code is a Ruby fallacy and should be avoided.
It does not name the arguments as they go in. It assigns two new variables into
the calling scope - very confusingly.

--
Phlip

TPReal

8/27/2008 7:35:00 AM

0

Kless wrote:
> it will change the order of args. and this is not desirable, how to
> solve it?

It will not. In Ruby, the order of passed arguments is the order of
received arguments, always. Your struct, as it was said, defines two
useless local variables, and in fact passes them to the function.
--
Posted via http://www.ruby-....