Meaning of *array

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

Hi –

On Sat, 25 Mar 2006, Axel Schlueter wrote:

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! Ruby for Rails

[email protected] wrote:

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

Ah! Thanks for the explanation.

Kind regards,
Axel

Axel Schlueter wrote:

[email protected] wrote:

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

Ah! Thanks for the explanation.

Kind regards,
Axel

It is mostly used as the end param to collect the overflow.

def one(o,t,*args)
puts o
puts t
puts args.join(‘,’)
end

one(1,2,2,2) =>
1
2
2,2

why_ talked about it on his blog a few weeks ago:
http://redhanded.hobix.com/bits/wonderOfTheWhenBeFlat.html

joey__

On 25 Mar 2006, at 02:09, [email protected] wrote:

does the asterisk do to the array ? Any hints
on where I can find a description in the
pickaxe ?

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:

    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

:slight_smile: 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.

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.

On 3/25/06, Benjohn B. [email protected] wrote:

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

I like this way better:

require ‘forwardable’

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

Hi –

On Sat, 25 Mar 2006, Benjohn B. wrote:

irb(main):006:0> a_function(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! Ruby for Rails

On 25 Mar 2006, at 19:42, Caleb C. wrote:

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

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

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

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[i])
    end
  end
  # more...
end

On 3/26/06, Benjohn B. [email protected] wrote:

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

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.