Deconstruction of Array Parameters by Functions

S

sickfaichezi

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

Joel VanderWerf

sickfaichezi said:
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.
 
D

David Balmain

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)
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top