How to pass a group of arguments?


#1

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])


#2

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 )


#3

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


#4

See page 332, “Method Arguments” and the following section “Invoking a
Method”.


#5

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?


#6

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.


#7

It is 'splained on p.80, “Variable-Length Argument Lists.”


#8

This is covered on p. 83 “Expanding Arrays in Method Calls.”


#9

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


#10

Thank you for finding me the page. So it is just a bit of ruby grammar
I had missed. thank you

anne