Expanding array as function arguments

Hi!
I wonder if it is possible to expand array as function arguments. Like

def foo( a, b, c)
end

foo [1,2,3].to_args

Is possible to do it?


Witold R.
nhw.pl (EN blog)
http://FriendsFeedMe.com

On Dec 5, 2007 1:43 PM, Witold R. [email protected] wrote:

Hi!
I wonder if it is possible to expand array as function arguments. Like

def foo( a, b, c)
end

foo [1,2,3].to_args

Is possible to do it?

foo *[1,2,3]

(
similar to grouping several arguments into an array if method
definition:

def foo(*args)
end

foo 1, 2, 3
)

Sergio Gil Pérez de la Manga wrote:

foo *[1,2,3]

(
similar to grouping several arguments into an array if method
definition:

def foo(*args)
end

foo 1, 2, 3
)

Thnx a lot :wink:

2007/12/5, Witold R. [email protected]:

Hi!
I wonder if it is possible to expand array as function arguments. Like

def foo( a, b, c)
end

foo [1,2,3].to_args

Is possible to do it?

foo *[1,2,3]

robert

Hi!
I wonder if it is possible to expand array as function arguments. Like

def foo( a, b, c)
end

foo [1,2,3].to_args
Is possible to do it?

How about foo [1, 2, 3] ? (Note the "" -“splat”).

On Dec 5, 7:13 am, Witold R. [email protected] wrote:

foo 1, 2, 3
)

Thnx a lot :wink:

Posted viahttp://www.ruby-forum.com/.

A note on splat; it can be used to both gather and unpack elements,
depending on the context. For example…

def baz(a, b, c)
p a, b, c
end
def foo(*bar) # gathering - bar = [1, 2, 3]
baz(*bar) # unpacking - bar = 1, 2, 3
end
foo(1, 2, 3)

Regards,
Jordan