Forum: Ruby How to pass a group of arguments?

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.
anne001 (Guest)
on 2006-02-20 03:19
(Received via mailing list)
I want to use a function which expects three arguments
GL.Scale(2.0, 0.4, 1.0);

I assumed I could put the parameters in an array
scale=[2.0, 0.4, 1.0]
pass it to an object
 def initialize(scale)
    @scale = scale
 end
and have a method call the function
GL.Scale(@scale);

but GL.Scale expects 3 arguments, and @scale counts as one.
What is the way to pass a group of argument? It would be uggly code
to write something like
GL.Scale(@scale[0],@scale[1],@scale[2])
Gregory B. (Guest)
on 2006-02-20 03:22
(Received via mailing list)
On 2/19/06, anne001 <removed_email_address@domain.invalid> wrote:

> but GL.Scale expects 3 arguments, and @scale counts as one.
> What is the way to pass a group of argument? It would be uggly code
> to write something like
> GL.Scale(@scale[0],@scale[1],@scale[2])

some_method( *some_ary )
Daniel H. (Guest)
on 2006-02-20 03:29
(Received via mailing list)
On Feb 20, 2006, at 2:18 AM, anne001 wrote:

> I want to use a function which expects three arguments
> GL.Scale(2.0, 0.4, 1.0);

Use the splat operator, "*".

GL.Scale(*scale_arr)

-- Daniel
anne001 (Guest)
on 2006-02-20 17:21
(Received via mailing list)
Thank you for your responses. The error does go away. I can't find a
reference to this is the programming ruby ed 2, is this * an array
method?
(Guest)
on 2006-02-20 17:30
(Received via mailing list)
See page 332, "Method Arguments" and the following section "Invoking a
Method".
Paul N. (Guest)
on 2006-02-20 17:33
(Received via mailing list)
It is 'splained on p.80, "Variable-Length Argument Lists."
anne001 (Guest)
on 2006-02-20 18:43
(Received via mailing list)
Thank you for your responses.
I had found the use of * to define a method, so the method will accept
variable number of arguments.

But I think that is different from the usage Gregory B. and Daniel
Harple suggested. There the * is used in front of an array passed on to
a function already defined.
Gregory B. (Guest)
on 2006-02-20 22:11
(Received via mailing list)
On 2/20/06, anne001 <removed_email_address@domain.invalid> wrote:
> Thank you for your responses.
> I had found the use of * to define a method, so the method will accept
> variable number of arguments.
>
> But I think that is different from the usage Gregory B. and Daniel
> Harple suggested. There the * is used in front of an array passed on to
> a function already defined.

sandal@harmonix:~$ irb
irb(main):001:0> def something(a,b,c)
irb(main):002:1> puts "#{a},#{b},#{c}"
irb(main):003:1> end
=> nil
irb(main):004:0> something(*[1,2,3])
1,2,3
Paul N. (Guest)
on 2006-02-20 22:29
(Received via mailing list)
This is covered on p. 83 "Expanding Arrays in Method Calls."
anne001 (Guest)
on 2006-02-21 01:06
(Received via mailing list)
Thank you for finding me the page. So it is just a bit of ruby grammar
I had missed. thank you

anne
This topic is locked and can not be replied to.