Deconstruction of Array Parameters by Functions

Discussion in 'Ruby' started by sickfaichezi, Sep 22, 2006.

  1. sickfaichezi

    sickfaichezi Guest

    Hi,

    I would like to pass arguments to a function that takes a variable
    number of arguments using an array.

    For example:

    my_array = [1, 2, 3]

    # Below should call my_var_arg_function(1, 2, 3)
    pass_array_as_args(my_var_arg_function, my_array)

    Is this possible in Ruby?

    Thanks in advance...
     
    sickfaichezi, Sep 22, 2006
    #1
    1. Advertising

  2. sickfaichezi wrote:
    > Hi,
    >
    > I would like to pass arguments to a function that takes a variable
    > number of arguments using an array.
    >
    > For example:
    >
    > my_array = [1, 2, 3]
    >
    > # Below should call my_var_arg_function(1, 2, 3)
    > pass_array_as_args(my_var_arg_function, my_array)


    my_var_arg_function(*my_array)

    It's called the "splat" or "unary un-array" operator.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Sep 22, 2006
    #2
    1. Advertising

  3. On 9/23/06, sickfaichezi <> wrote:
    > Hi,
    >
    > I would like to pass arguments to a function that takes a variable
    > number of arguments using an array.
    >
    > For example:
    >
    > my_array = [1, 2, 3]
    >
    > # Below should call my_var_arg_function(1, 2, 3)
    > pass_array_as_args(my_var_arg_function, my_array)
    >
    > Is this possible in Ruby?
    >
    > Thanks in advance...
    >


    Something like this?

    def three(arg1, arg2, arg3)
    puts "#{arg1}-#{arg2}-#{arg3}"
    end

    args = [1,2,3]
    three(*args)
     
    David Balmain, Sep 22, 2006
    #3
    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. Jonck van der Kogel
    Replies:
    2
    Views:
    1,010
    Jonck van der Kogel
    May 27, 2004
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,663
    Victor Bazarov
    Jun 21, 2004
  3. Jason
    Replies:
    2
    Views:
    556
    Jonathan Mcdougall
    May 13, 2006
  4. Christopher
    Replies:
    9
    Views:
    887
    Kai-Uwe Bux
    Feb 28, 2009
  5. Replies:
    8
    Views:
    121
    Tad J McClellan
    Sep 9, 2008
Loading...

Share This Page