[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to pass a group of arguments?

anne001

2/20/2006 1:17:00 AM

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

8 Answers

Gregory Brown

2/20/2006 1:22:00 AM

0

On 2/19/06, anne001 <anne@wjh.harvard.edu> 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 Harple

2/20/2006 1:26:00 AM

0

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

2/20/2006 3:16:00 PM

0

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?

Tim Hunter

2/20/2006 3:27:00 PM

0

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

Paul Novak

2/20/2006 3:31:00 PM

0

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

On 2/20/06, anne001 <anne@wjh.harvard.edu> wrote:
> 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?
>
>
>


anne001

2/20/2006 4:41:00 PM

0

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 Brown and Daniel
Harple suggested. There the * is used in front of an array passed on to
a function already defined.

Paul Novak

2/20/2006 8:26:00 PM

0

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

On 2/20/06, anne001 <anne@wjh.harvard.edu> 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 Brown and Daniel
> Harple suggested. There the * is used in front of an array passed on to
> a function already defined.
>
>
>


anne001

2/20/2006 11:02:00 PM

0

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

anne