Order of arguments

J

Joel VanderWerf

Kless said:
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)
 
P

Phlip

Kless said:
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.
 
T

Thomas B.

Kless said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top