Expanding array as function arguments

  • Thread starter Witold Rugowski
  • Start date
W

Witold Rugowski

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?
 
S

Sergio Gil Pérez de la Manga

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
)

--=20
Sergio Gil P=E9rez de la Manga
e-mail > (e-mail address removed)
blog > http://www.lacoctelera.com/porras
 
R

Robert Klemme

2007/12/5 said:
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
 
R

Rimantas Liubertas

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").
 
W

Witold Rugowski

Sergio said:
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 ;)
 
M

MonkeeSage

Sergio said:
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 ;)

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
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top