# Meaning of \*array

**URL:** <https://rubytalk.org/t/meaning-of-array/26084>\
**Category:** ruby-talk\
**Created:** [25 March 2006 01:54 UTC](https://rubytalk.org/t/meaning-of-array/26084 "2006-03-25T01:54:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axel\_Schlueter](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Axel\_Schlueter](https://rubytalk.org/u/Axel_Schlueter)\
**Post date:** [25 March 2006 01:54 UTC](https://rubytalk.org/t/meaning-of-array/26084/1 "2006-03-25T01:54:41Z")

</div>

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

> **···**
>
> --  
> Friendship is unnecessary, like philosophy and art. It has no  
> survival value; rather, it is one of those things that give  
> value to survival. -- C.S.Lewis

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 March 2006 02:09 UTC](https://rubytalk.org/t/meaning-of-array/26084/2 "2006-03-25T02:09:12Z")

</div>

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

> **···**
>
> On Sat, 25 Mar 2006, Axel Schlueter wrote:
> 
> --  
> David A. Black (dblack@wobblini.net)  
> Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))
> 
> "Ruby for Rails" chapters now available  
> from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![Axel\_Schlueter](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Axel\_Schlueter](https://rubytalk.org/u/Axel_Schlueter)\
**Post date:** [25 March 2006 02:28 UTC](https://rubytalk.org/t/meaning-of-array/26084/3 "2006-03-25T02:28:53Z")

</div>

dblack@wobblini.net 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

> **···**
>
> --  
> Friendship is unnecessary, like philosophy and art. It has no  
> survival value; rather, it is one of those things that give  
> value to survival. -- C.S.Lewis

---

<div class="post-metadata">

**Author:** ![Benjohn\_Barnes](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Benjohn\_Barnes](https://rubytalk.org/u/Benjohn_Barnes)\
**Post date:** [25 March 2006 10:33 UTC](https://rubytalk.org/t/meaning-of-array/26084/4 "2006-03-25T10:33:35Z")

</div>

> 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 ?

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 )  
&nbsp;&nbsp;puts required\_argument  
&nbsp;&nbsp;puts also\_required  
&nbsp;&nbsp;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)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):9:in `f'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;attr\_accessor :my\_delegatee

...  
&nbsp;&nbsp;# Pick method calls I can't handle, and pass them on to the delegatee.  
&nbsp;&nbsp;def method\_missing( method\_name, \*all\_the\_parameters )  
&nbsp;&nbsp;&nbsp;&nbsp;@my\_delegatee.send( method\_name, \*all\_the\_parameters )  
&nbsp;&nbsp;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 )  
&nbsp;&nbsp;@my\_delegatee.send( method\_name, \*all\_the\_parameters, &optional\_block )  
end

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

> **···**
>
> On 25 Mar 2006, at 02:09, dblack@wobblini.net wrote:
> 
> > On Sat, 25 Mar 2006, Axel Schlueter wrote:

---

<div class="post-metadata">

**Author:** ![j\_ey](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@j\_ey](https://rubytalk.org/u/j_ey)\
**Post date:** [25 March 2006 07:40 UTC](https://rubytalk.org/t/meaning-of-array/26084/5 "2006-03-25T07:40:08Z")

</div>

Axel Schlueter 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)  
&nbsp;&nbsp;puts o  
&nbsp;&nbsp;puts t  
&nbsp;&nbsp;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](http://redhanded.hobix.com/bits/wonderOfTheWhenBeFlat.html)

joey\_\_

> **···**
>
> > dblack@wobblini.net wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Anne\_G](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Anne\_G](https://rubytalk.org/u/Anne_G)\
**Post date:** [25 March 2006 10:43 UTC](https://rubytalk.org/t/meaning-of-array/26084/6 "2006-03-25T10:43:53Z")

</div>

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)

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

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

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 March 2006 11:51 UTC](https://rubytalk.org/t/meaning-of-array/26084/7 "2006-03-25T11:51:55Z")

</div>

Hi --

> **···**
>
> On Sat, 25 Mar 2006, Benjohn Barnes wrote:
> 
> > 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 )  
> > &nbsp;&nbsp;puts required\_argument  
> > &nbsp;&nbsp;puts also\_required  
> > &nbsp;&nbsp;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:
> 
> &nbsp;&nbsp;&nbsp;list,of,things \<=\> \*array
> 
> In both cases, the star "translates" between an array and a list of  
> things.
> 
> David
> 
> --  
> David A. Black (dblack@wobblini.net)  
> Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))
> 
> "Ruby for Rails" chapters now available  
> from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![Caleb\_Clausen1](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@Caleb\_Clausen1](https://rubytalk.org/u/Caleb_Clausen1)\
**Post date:** [25 March 2006 19:42 UTC](https://rubytalk.org/t/meaning-of-array/26084/8 "2006-03-25T19:42:12Z")

</div>

I like this way better:

require 'forwardable'

class WhateverItIs  
&nbsp;&nbsp;extend Forwardable  
&nbsp;&nbsp;def\_delegator :@my\_delegatee, :method\_missing  
end

> **···**
>
> On 3/25/06, Benjohn Barnes \<benjohn@fysh.org\> wrote:
> 
> > def method\_missing( method\_name, \*all\_the\_parameters, &optional\_block )  
> > &nbsp;&nbsp;@my\_delegatee.send( method\_name, \*all\_the\_parameters, &optional\_block )  
> > end

---

<div class="post-metadata">

**Author:** ![Benjohn\_Barnes](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Benjohn\_Barnes](https://rubytalk.org/u/Benjohn_Barnes)\
**Post date:** [26 March 2006 09:02 UTC](https://rubytalk.org/t/meaning-of-array/26084/9 "2006-03-26T09:02:09Z")

</div>

🙂 🙂 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):

&nbsp;&nbsp;[http://www.rubycentral.com/book/lib\_patterns.html](http://www.rubycentral.com/book/lib_patterns.html)

Cheers,  
&nbsp;&nbsp;Benjohn

> **···**
>
> On 25 Mar 2006, at 19:42, Caleb Clausen wrote:
> 
> > On 3/25/06, Benjohn Barnes \<benjohn@fysh.org\> wrote:
> > 
> > > def method\_missing( method\_name, \*all\_the\_parameters, &optional\_block )  
> > > &nbsp;&nbsp;@my\_delegatee.send( method\_name, \*all\_the\_parameters, &optional\_block )  
> > > end
> > 
> > I like this way better:
> > 
> > require 'forwardable'
> > 
> > class WhateverItIs  
> > &nbsp;&nbsp;extend Forwardable  
> > &nbsp;&nbsp;def\_delegator :@my\_delegatee, :method\_missing  
> > end

---

<div class="post-metadata">

**Author:** ![Anne\_G](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Anne\_G](https://rubytalk.org/u/Anne_G)\
**Post date:** [26 March 2006 12:28 UTC](https://rubytalk.org/t/meaning-of-array/26084/10 "2006-03-26T12:28:54Z")

</div>

here is an example using \*  
[http://www.devsource.com/article2/0,1895,1928562,00.asp](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

&nbsp;&nbsp;&nbsp;klass.class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor \*names

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:initialize) do |\*values|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;names.each\_with\_index do |name,i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set("@"+name, values[i])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# more...  
&nbsp;&nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Caleb\_Clausen1](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@Caleb\_Clausen1](https://rubytalk.org/u/Caleb_Clausen1)\
**Post date:** [26 March 2006 16:43 UTC](https://rubytalk.org/t/meaning-of-array/26084/11 "2006-03-26T16:43:03Z")

</div>

> 🙂 🙂 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.

> **···**
>
> On 3/26/06, Benjohn Barnes \<benjohn@fysh.org\> wrote:
