[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Deconstruction of Array Parameters by Functions

sickfaichezi

9/22/2006 7:10:00 PM

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...

2 Answers

Joel VanderWerf

9/22/2006 7:21:00 PM

0

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

David Balmain

9/22/2006 7:21:00 PM

0

On 9/23/06, sickfaichezi <sickfaichezi@gmail.com> 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)