Forum: Ruby Array into list of arguments - nicest way?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2009-02-23 12:45
hey all

I want to pass the contents of an array to a method, to be interpreted
as a standard list of arguments.  What's the simplest and nicest way of
doing this?  (i can think of a few dirty hack ways but don't want to use
them)

eg

#have
arr = [arg1, arg2, arg3]
#want
a_method(arg1, arg2, arg3)
B32a1911d783e0a95e0b062fd5d0b64b?d=identicon&s=25 Jan Friedrich (janfri)
on 2009-02-23 12:52
(Received via mailing list)
Max Williams <toastkid.williams@gmail.com> wrote:
> #have
> arr = [arg1, arg2, arg3]
> #want
> a_method(arg1, arg2, arg3)

a_method(*arr)

Regards,
Jan Friedrich
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-02-23 12:57
(Received via mailing list)
On Mon, Feb 23, 2009 at 12:44 PM, Max Williams
<toastkid.williams@gmail.com> wrote:
> arr = [arg1, arg2, arg3]
> #want
> a_method(arg1, arg2, arg3)

Look for documentation, etc for the splat operator (there's plenty out
there):

a_method (*arr)

Jesus.
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-02-23 13:17
(Received via mailing list)
Max Williams wrote:
> arr = [arg1, arg2, arg3]
> #want
> a_method(arg1, arg2, arg3)

Look up the splat operator, a.k.a. unary unarray:

a_method(*arr)
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2009-02-23 13:29
aha, that makes sense...

thanks a lot, everyone.  I've used the splat before at the end of
parameter lists, eg

def a_method(a_thing, *args)

But i guess i didn't really properly understand what it does.  I can't
find an explaanation in pickaxe, although it's not the simplest thing to
look for in the index (there's no listing for 'splat' and a lot of
listings for '*').
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-02-23 13:37
Max Williams wrote:
> But i guess i didn't really properly understand what it does.  I can't
> find an explaanation in pickaxe, although it's not the simplest thing to
> look for in the index (there's no listing for 'splat' and a lot of
> listings for '*').

http://www.ruby-doc.org/docs/ProgrammingRuby/

Click on the chapter "More about Methods"

Scroll down to the section headed "Expanding Arrays in Method Calls"

It could be indexed better :-)
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2009-02-23 13:55
Brian Candler wrote:
> Max Williams wrote:
>> But i guess i didn't really properly understand what it does.  I can't
>> find an explaanation in pickaxe, although it's not the simplest thing to
>> look for in the index (there's no listing for 'splat' and a lot of
>> listings for '*').
>
> http://www.ruby-doc.org/docs/ProgrammingRuby/
>
> Click on the chapter "More about Methods"
>
> Scroll down to the section headed "Expanding Arrays in Method Calls"
>
> It could be indexed better :-)

Cool.  I'm using the paper version, it's page 83 :)

Thanks a lot.
This topic is locked and can not be replied to.