Expanding array as function arguments - RUBY
This is a discussion on Expanding array as function arguments - RUBY ; 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 Rugowski
http://nhw.pl/wp/ (EN blog)
http://FriendsFeedMe.com
--
Posted via http://www.ruby-forum.com/ ....
-
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 Rugowski
http://nhw.pl/wp/ (EN blog)
http://FriendsFeedMe.com
--
Posted via http://www.ruby-forum.com/.
-
Re: Expanding array as function arguments
On Dec 5, 2007 1:43 PM, Witold Rugowski <rugowski@nhw.pl> 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
)
--=20
Sergio Gil P=E9rez de la Manga
e-mail > sgilperez@gmail.com
blog > http://www.lacoctelera.com/porras
-
Re: Expanding array as function arguments
2007/12/5, Witold Rugowski <rugowski@nhw.pl>:
> 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
--
use.inject do |as, often| as.you_can - without end
-
Re: 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?
How about foo *[1, 2, 3] ? (Note the "*" -"splat").
--
Regards,
Rimantas
--
http://rimantas.com/
-
Re: Expanding array as function arguments
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 
--
Posted via http://www.ruby-forum.com/.
-
Re: Expanding array as function arguments
On Dec 5, 7:13 am, Witold Rugowski <rugow...@nhw.pl> wrote:
> 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 
> --
> 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
Similar Threads
-
By Application Development in forum Javascript
Replies: 18
Last Post: 11-24-2007, 05:10 PM
-
By Application Development in forum Python
Replies: 1
Last Post: 11-03-2007, 03:20 AM
-
By Application Development in forum PHP
Replies: 3
Last Post: 08-13-2007, 07:21 AM
-
By Application Development in forum Idl-pvwave
Replies: 2
Last Post: 11-23-2006, 02:18 PM
-
By Application Development in forum Perl
Replies: 2
Last Post: 06-11-2004, 06:12 AM