Order of arguments

Discussion in 'Ruby' started by Kless, Aug 26, 2008.

  1. Kless

    Kless Guest

    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
     
    Kless, Aug 26, 2008
    #1
    1. Advertising

  2. 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
     
    Joel VanderWerf, Aug 26, 2008
    #2
    1. Advertising

  3. Kless

    Phlip Guest

    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
     
    Phlip, Aug 27, 2008
    #3
  4. Kless

    Thomas B. Guest

    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/.
     
    Thomas B., Aug 27, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Diener
    Replies:
    14
    Views:
    4,975
    Josiah Carlson
    Apr 6, 2004
  2. Neo
    Replies:
    10
    Views:
    670
    sushant
    Jan 20, 2005
  3. tutmann
    Replies:
    4
    Views:
    439
  4. jmborr
    Replies:
    1
    Views:
    424
    Stargaming
    Nov 3, 2007
  5. Replies:
    3
    Views:
    829
Loading...

Share This Page