meaning of *array

A

Axel Schlueter

Hi,

When reading somes ruby source I saw the
*array_name notation, an asterisk in front
of an array variables. I was unable to look
up its meaning in the pickaxe. What exactly
does the asterisk do to the array ? Any hints
on where I can find a description in the
pickaxe ?

Kind regards,
Axel
 
D

dblack

Hi --

Hi,

When reading somes ruby source I saw the
*array_name notation, an asterisk in front
of an array variables. I was unable to look
up its meaning in the pickaxe. What exactly
does the asterisk do to the array ? Any hints
on where I can find a description in the
pickaxe ?

The * is a "unary unarray" operator. It "un-arrays" the array into a
list of separate values.

Thus, for example:

a = [4,5,6]
[1,2,3,*a] => [1,2,3,4,5,6]

There are rules about where and when and how it can be used, but
that's basically what's happening when you see that.


David

--
David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 
B

Benjohn Barnes

--Apple-Mail-9--380644935
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed



There's something about it near the beginning, in the section about
passing arguments to functions. In fact, until the post below, I had
no idea it could be used more generally!
The * is a "unary unarray" operator. It "un-arrays" the array into a
list of separate values.

Thus, for example:

a = [4,5,6]
[1,2,3,*a] => [1,2,3,4,5,6]

There are rules about where and when and how it can be used, but
that's basically what's happening when you see that.

That makes a lot of sense, thank you! Usually this will be used to
allow a function to take additional arguments:

def a_function( required_argument, also_required,
*any_optional_arguments )
puts required_argument
puts also_required
puts any_optional_arguments.join(',')
end

Calling:
irb(main):006:0> a_function(1,2,3,4,5)
1
2
3,4,5

You can also use it to split out an array in to seperate parameters
(so kind of the opposite of the above). Again, this is often used
when calling a method:
irb(main):007:0> a=[1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):008:0> a_function(*a)
1
2
3,4,5
=> nil
irb(main):009:0> a_function(a)
ArgumentError: wrong number of arguments (1 for 2)
from (irb):9:in `f'
from (irb):9

As an example of both of these uses; I'd like to have an object
delegate any method calls that it can't handle to some other object.
I could write it like this:

class DelgatingClass
attr_accessor :my_delegatee

...
# Pick method calls I can't handle, and pass them on to the delegatee.
def method_missing( method_name, *all_the_parameters )
@my_delegatee.send( method_name, *all_the_parameters )
end
...
end

:) However, the above doesn't pick up any optional block that's
passed to the method. To rectify that, you would probably write the
method above like this:

def method_missing( method_name, *all_the_parameters, &optional_block )
@my_delegatee.send( method_name, *all_the_parameters, &optional_block )
end

I think this is also discussed in the same section of the pickaxe.


--Apple-Mail-9--380644935--
 
A

anne001

I used it with opengl. Opengl fx expect arrays of 3 or 4 vectors
(location of points in space, color, etc)

so I entered my data in arrays
scale=[2.0, 0.3, 1.0]
translation=[0.0, 0.0, 0.0]
jointP = [-scale[0]/2, 0, 0]
rotation = [$elbow, 0.0, 0.0, 1.0]
draw = proc { GLUT.WireCube(1.0)}
lowerarm= Node.new(scale,translation, jointP, rotation,&draw)

def initialize(scale, translation, jointP, rotation, &draw)
@children = []
@scale = scale
@Translation = translation
@jointP = jointP
@rotation = rotation
@draw = draw
end

but then when I called GL functions, I had to use *
def applytransform
GL.PushMatrix();
GL.Translate(*@Translation);
if @parent != nil
GL.Translate(*@parentjointP);
end
GL.Rotate(*@rotation);
GL.Translate(*@jointP.minus);
end

Otherwise the GL fx complained it did not get the number of parameters
it wanted.
 
D

dblack

Hi --

That makes a lot of sense, thank you! Usually this will be used to allow a
function to take additional arguments:

def a_function( required_argument, also_required, *any_optional_arguments )
puts required_argument
puts also_required
puts any_optional_arguments.join(',')
end

Calling:
irb(main):006:0> a_function(1,2,3,4,5)
1
2
3,4,5

You can also use it to split out an array in to seperate parameters (so kind
of the opposite of the above). Again, this is often used when calling a
method:
irb(main):007:0> a=[1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):008:0> a_function(*a)
1
2
3,4,5

The two uses seem opposite, in a sense, but they're actually the same,
if you think of the following equivalency:

list,of,things <=> *array

In both cases, the star "translates" between an array and a list of
things.


David

--
David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 
C

Caleb Clausen

def method_missing( method_name, *all_the_parameters, &optional_block )
=09@my_delegatee.send( method_name, *all_the_parameters, &optional_block = )
end=09

I like this way better:

require 'forwardable'

class WhateverItIs
extend Forwardable
def_delegator :mad:my_delegatee, :method_missing
end
 
B

Benjohn Barnes

I like this way better:

require 'forwardable'

class WhateverItIs
extend Forwardable
def_delegator :mad:my_delegatee, :method_missing
end

:) :) So do I too. It was an example, and in no way intended to show
regular practice!!!! :)

I should have given a link to that when I mentioned it. Caleb is
absolutely right. You should never use the code I posted as anything
more than a guide of how something like that could work, and if you
have been misguided by me, please accept my abject apologies.

You could also look at the Ruby standard library 'delegate',
documentation for which can be found here (Thanks Dave et al):

http://www.rubycentral.com/book/lib_patterns.html

Cheers,
Benjohn
 
A

anne001

here is an example using *
http://www.devsource.com/article2/0,1895,1928562,00.asp
see the text for explanation of what it does

data = File.new(file_name)
names = data.gets.chomp.split(",") # an array of strings

klass.class_eval do
attr_accessor *names

define_method:)initialize) do |*values|
names.each_with_index do |name,i|
instance_variable_set("@"+name, values)
end
end
# more...
end
 
C

Caleb Clausen

:) :) So do I too. It was an example, and in no way intended to show
regular practice!!!! :)

I realized this after I posted. It seems like def_delegator(s) isn't
widely enough known, and I thought this could be a good opportunity to
evangalize it, and golf a little. I did sort of miss the point of this
thread by optimizing the * away completely.
I should have given a link to that when I mentioned it. Caleb is
absolutely right. You should never use the code I posted as anything
more than a guide of how something like that could work, and if you
have been misguided by me, please accept my abject apologies.

Well, you weren't misguided. Either way should work perfectly well. I
would even say your way is probably more commonly found in the wild
than mine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,668
Latest member
SamiraShac

Latest Threads

Top