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

+ Reply to Thread
Results 1 to 6 of 6

Expanding array as function arguments

  1. Default 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/.


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


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


  4. Default 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/


  5. Default 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/.


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

+ Reply to Thread

Similar Threads

  1. function arguments
    By Application Development in forum Javascript
    Replies: 18
    Last Post: 11-24-2007, 05:10 PM
  2. Replies: 1
    Last Post: 11-03-2007, 03:20 AM
  3. Replies: 3
    Last Post: 08-13-2007, 07:21 AM
  4. Expanding array with counts from another array
    By Application Development in forum Idl-pvwave
    Replies: 2
    Last Post: 11-23-2006, 02:18 PM
  5. Expanding scalar hash element in array ref.
    By Application Development in forum Perl
    Replies: 2
    Last Post: 06-11-2004, 06:12 AM