Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Order of arguments (http://www.velocityreviews.com/forums/t852533-order-of-arguments.html)

Kless 08-26-2008 10:43 PM

Order of arguments
 
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

Joel VanderWerf 08-26-2008 10:48 PM

Re: Order of arguments
 
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 08-27-2008 12:47 AM

Re: Order of arguments
 
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

Thomas B. 08-27-2008 07:35 AM

Re: Order of arguments
 
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-forum.com/.



All times are GMT. The time now is 08:02 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.