# Ruby "star".... what's it?

**URL:** <https://rubytalk.org/t/ruby-star-whats-it/54555>\
**Category:** ruby-talk\
**Created:** [23 July 2009 07:47 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555 "2009-07-23T07:47:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mirko\_Viviani](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Mirko\_Viviani](https://rubytalk.org/u/Mirko_Viviani)\
**Post date:** [23 July 2009 07:47 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/1 "2009-07-23T07:47:42Z")

</div>

Hi everybody.  
What means this snippet of code? Why use the "star" ?

myarray = [1,2,3,4,5,6,6]  
qa = \*myarray

Thank you.

> **···**
>
> --  
> -- Mirko Viviani --  
> GPG-PGP Public Key: 0xE4E8FAB1  
> Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> "“Machines take me by surprise with great frequency.” A. Turing

---

<div class="post-metadata">

**Author:** ![Max\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Max\_Williams](https://rubytalk.org/u/Max_Williams)\
**Post date:** [23 July 2009 09:14 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/2 "2009-07-23T09:14:50Z")

</div>

Mirko Viviani wrote:

> Hi everybody.  
> What means this snippet of code? Why use the "star" ?
> 
> myarray = [1,2,3,4,5,6,6]  
> qa = \*myarray
> 
> Thank you.

In this example it doesn't really do anything, i think. Normally you  
would use the \* in an argument list, in a method definition, to  
effectively say 'pull all arguments after this into a single array', eg

> > def foo(mainarg, \*otherargs)  
> > &nbsp;&nbsp;puts mainarg.inspect  
> > &nbsp;&nbsp;puts otherargs.inspect  
> > end

=\> nil

> > foo(1,2,3,4,5,6)

1  
[2, 3, 4, 5, 6]  
=\> nil

In this sense it's like \* in a regular expression, meaning 'any number  
of arguments'. That's how i understand it anyway, a more precise  
description will probably be forthcoming 🙂

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [23 July 2009 10:56 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/3 "2009-07-23T10:56:47Z")

</div>

Hi --

> Hi everybody.  
> What means this snippet of code? Why use the "star" ?
> 
> myarray = [1,2,3,4,5,6,6]  
> qa = \*myarray

In addition to the examples in the other answers, note that the star  
has some new behavior in 1.9.

Here's the 1.8 behavior.

> > \*a = 1; a

=\> [1]

> > \*a = [1]; a

=\> [[1]]

> > \*a = [[1]]; a

=\> [[[1]]]

In each case, the star indicates one "missing" level of array  
wrapping. In 1.9, that's changed:

> > \*a = 1; a

=\> [1]

> > \*a = [1]; a

=\> [1]

> > \*a = [[1]]; a

=\> [[1]]

\*a = 1 and \*a = [1] have the same result. (I know there was a lot of  
discussion about this when it was being changed but I can't remember  
exactly what the rationale was.)

David

> **···**
>
> On Thu, 23 Jul 2009, Mirko Viviani wrote:
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Now available: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))  
> Training! Intro to Ruby, with Black & Kastner, September 14-17  
> (More info: [http://rubyurl.com/vmzN\](http://rubyurl.com/vmzN%5C))

---

<div class="post-metadata">

**Author:** ![Mirko\_Viviani](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Mirko\_Viviani](https://rubytalk.org/u/Mirko_Viviani)\
**Post date:** [23 July 2009 09:23 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/4 "2009-07-23T09:23:11Z")

</div>

Sorry,  
Complete code was like this  
x, \*xs = \*myarray

after executing, x is 1 and xs [2,3,4,5,6,6] ..... :o

> **···**
>
> 2009/7/23 Max Williams \<toastkid.williams@gmail.com\>
> 
> > Mirko Viviani wrote:  
> > \> Hi everybody.  
> > \> What means this snippet of code? Why use the "star" ?  
> > \>  
> > \> myarray = [1,2,3,4,5,6,6]  
> > \> qa = \*myarray  
> > \>  
> > \> Thank you.
> > 
> > In this example it doesn't really do anything, i think. Normally you  
> > would use the \* in an argument list, in a method definition, to  
> > effectively say 'pull all arguments after this into a single array', eg
> > 
> > \>\> def foo(mainarg, \*otherargs)  
> > \>\> puts mainarg.inspect  
> > \>\> puts otherargs.inspect  
> > \>\> end  
> > =\> nil  
> > \>\> foo(1,2,3,4,5,6)  
> > 1  
> > [2, 3, 4, 5, 6]  
> > =\> nil
> > 
> > In this sense it's like \* in a regular expression, meaning 'any number  
> > of arguments'. That's how i understand it anyway, a more precise  
> > description will probably be forthcoming 🙂  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> -- Mirko Viviani --  
> GPG-PGP Public Key: 0xE4E8FAB1  
> Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> "“Machines take me by surprise with great frequency.” A. Turing

---

<div class="post-metadata">

**Author:** ![Marvin\_Gulker1](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@Marvin\_Gulker1](https://rubytalk.org/u/Marvin_Gulker1)\
**Post date:** [23 July 2009 09:41 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/5 "2009-07-23T09:41:14Z")

</div>

Mirko Viviani schrieb:

> Sorry,  
> Complete code was like this  
> x, \*xs = \*myarray
> 
> after executing, x is 1 and xs [2,3,4,5,6,6] ..... :o
> 
> > Mirko Viviani wrote:  
> > &nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > > Hi everybody.  
> > > What means this snippet of code? Why use the "star" ?
> > > 
> > > myarray = [1,2,3,4,5,6,6]  
> > > qa = \*myarray
> > > 
> > > Thank you.  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > In this example it doesn't really do anything, i think. Normally you  
> > would use the \* in an argument list, in a method definition, to  
> > effectively say 'pull all arguments after this into a single array', eg
> > 
> > > > def foo(mainarg, \*otherargs)  
> > > > &nbsp;&nbsp;puts mainarg.inspect  
> > > > &nbsp;&nbsp;puts otherargs.inspect  
> > > > end  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > =\> nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > > > foo(1,2,3,4,5,6)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > 1  
> > [2, 3, 4, 5, 6]  
> > =\> nil
> > 
> > In this sense it's like \* in a regular expression, meaning 'any number  
> > of arguments'. That's how i understand it anyway, a more precise  
> > description will probably be forthcoming 🙂  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

irb(main):001:0\> x = 0  
=\> 0  
irb(main):002:0\> xs = [0, 0]  
=\> [0, 0]  
irb(main):003:0\> myarray = [1, 2, 3]  
=\> [1, 2, 3]  
irb(main):004:0\> x, \*xs = \*myarray  
=\> [1, 2, 3]  
irb(main):005:0\> x  
=\> 1  
irb(main):006:0\> xs  
=\> [2, 3]  
irb(main):007:0\> x = 0  
=\> 0  
irb(main):008:0\> xs = [0, 0]  
=\> [0, 0]  
irb(main):009:0\> x, xs = myarray =\> [1, 2, 3]  
irb(main):010:0\> x  
=\> 1  
irb(main):011:0\> xs  
=\> 2

The \* is also called the "splat operator". It is used to split up an array in is parts or  
the other way round (like in the method definition already posted). You could also  
do things like:

def foo(x, y)  
&nbsp;&nbsp;puts x  
&nbsp;&nbsp;puts y  
end

ary = [1, 2]  
foo(ary) #=\> ArgumentError  
foo(\*ary) #=\> 1, 2

Marvin

> **···**
>
> > 2009/7/23 Max Williams \<toastkid.williams@gmail.com\>

---

<div class="post-metadata">

**Author:** ![Max\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Max\_Williams](https://rubytalk.org/u/Max_Williams)\
**Post date:** [23 July 2009 09:50 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/6 "2009-07-23T09:50:29Z")

</div>

Mirko Viviani wrote:

> Sorry,  
> Complete code was like this  
> x, \*xs = \*myarray
> 
> after executing, x is 1 and xs [2,3,4,5,6,6] ..... :o

Ah, well there you go. In ruby, you can assign multiple variables to  
multiple values simultaneously: effectively on the left side of the =  
you have a list of variables and on the right you have a list of values.  
If the sizes of the lists don't match up then any extra values are  
thrown away and any extra variables are set to nil. eg

However, if you use the \* on the rightmost variable, then any extra  
values get pulled into an array and that variable points to that array:

> > a, b, c = 1, 2, 3

=\> [1, 2, 3]

> > puts a.inspect; puts b.inspect; puts c.inspect

1  
2  
3  
=\> nil

> > a, b, c = 1, 2

=\> [1, 2]

> > puts a.inspect; puts b.inspect; puts c.inspect

1  
2  
nil  
=\> nil

> > a, b = 1, 2, 3

=\> [1, 2, 3]

> > puts a.inspect; puts b.inspect

1  
2  
=\> nil

> > a, \*b = 1, 2, 3

=\> [1, 2, 3]

> > puts a.inspect; puts b.inspect

1  
[2, 3]

> **···**
>
> > 2009/7/23 Max Williams \<toastkid.williams@gmail.com\>
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [23 July 2009 09:50 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/7 "2009-07-23T09:50:29Z")

</div>

Mirko Viviani wrote:

> Sorry,  
> Complete code was like this  
> x, \*xs = \*myarray
> 
> after executing, x is 1 and xs [2,3,4,5,6,6] ..... :o

x, y, z = 1, 2, 3  
puts x, y, z

--output:--  
1  
2  
3

x, y, z = \*[1, 2, 3]  
puts x, y, z

--output:--  
1  
2  
3

\*y = 1, 2, 3  
p y

--output:--  
[1, 2, 3]

x, \*y = \*[1, 2, 3]  
puts x  
p y

--output:--  
1  
[2, 3]

Depending on the context the \* means:

a) replace an array with its individual elements  
b) gather individual elements into an array

> **···**
>
> > 2009/7/23 Max Williams \<toastkid.williams@gmail.com\>
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Mirko\_Viviani](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Mirko\_Viviani](https://rubytalk.org/u/Mirko_Viviani)\
**Post date:** [23 July 2009 09:54 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/8 "2009-07-23T09:54:27Z")

</div>

thank's to all.... this is a think I would remember 😃

> **···**
>
> 2009/7/23 7stud -- \<bbxx789\_05ss@yahoo.com\>
> 
> > Mirko Viviani wrote:  
> > \> Sorry,  
> > \> Complete code was like this  
> > \> x, \*xs = \*myarray  
> > \>  
> > \> after executing, x is 1 and xs [2,3,4,5,6,6] ..... :o  
> > \>  
> > \> 2009/7/23 Max Williams \<toastkid.williams@gmail.com\>
> > 
> > x, y, z = 1, 2, 3  
> > puts x, y, z
> > 
> > --output:--  
> > 1  
> > 2  
> > 3
> > 
> > x, y, z = \*[1, 2, 3]  
> > puts x, y, z
> > 
> > --output:--  
> > 1  
> > 2  
> > 3
> > 
> > \*y = 1, 2, 3  
> > p y
> > 
> > --output:--  
> > [1, 2, 3]
> > 
> > x, \*y = \*[1, 2, 3]  
> > puts x  
> > p y
> > 
> > --output:--  
> > 1  
> > [2, 3]
> > 
> > Depending on the context the \* means:
> > 
> > a) replace an array with its individual elements  
> > b) gather individual elements into an array  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> -- Mirko Viviani --  
> GPG-PGP Public Key: 0xE4E8FAB1  
> Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> "“Machines take me by surprise with great frequency.” A. Turing

---

<div class="post-metadata">

**Author:** ![Max\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Max\_Williams](https://rubytalk.org/u/Max_Williams)\
**Post date:** [23 July 2009 10:11 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/9 "2009-07-23T10:11:46Z")

</div>

7stud -- wrote:

> Depending on the context the \* means:
> 
> a) replace an array with its individual elements  
> b) gather individual elements into an array

I said a more precise explanation would be forthcoming 🙂

7stud - am i right in thinking that if i do this

a, b = 1, 2

ruby is automatically starring just the right side of the expression,  
under the hood? eg

> > a,b = 1,2

=\> [1, 2]

> > puts a.inspect;puts b.inspect

1  
2  
=\> nil

> > a,b = [1,2]

=\> [1, 2]

> > puts a.inspect;puts b.inspect

1  
2  
=\> nil

> > [a,b] = [1,2]

SyntaxError: compile error  
(irb):37: syntax error, unexpected '=', expecting $end  
[a,b] = [1,2]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
&nbsp;&nbsp;from (irb):37

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [23 July 2009 11:01 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/10 "2009-07-23T11:01:31Z")

</div>

Hi --

> Depending on the context the \* means:
> 
> a) replace an array with its individual elements  
> b) gather individual elements into an array

It's changed in 1.9. \* on the left no longer has the same "un-array"  
semantics.

> > a = [1]; a

=\> [1]

> > \*a = [1]; a

=\> [1]

I'm kind of drawing a blank on why this change was made.

David

> **···**
>
> On Thu, 23 Jul 2009, 7stud -- wrote:
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Now available: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))  
> Training! Intro to Ruby, with Black & Kastner, September 14-17  
> (More info: [http://rubyurl.com/vmzN\](http://rubyurl.com/vmzN%5C))

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [23 July 2009 11:32 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/11 "2009-07-23T11:32:58Z")

</div>

Max Williams wrote:

> 7stud - am i right in thinking that if i do this
> 
> a, b = 1, 2
> 
> ruby is automatically starring just the right side of the expression,  
> under the hood? eg

In pickaxe2, p. 91, the rule is:

> **···**
>
> ----  
> When an assignment has more than one lvalue, the assignment expression  
> returns an array of the rvalues. Testing that out:  
> ----
> 
> result = (x, y = [1], [2, 3])  
> p result
> 
> So I guess you could say "ruby is automatically starring the right hand  
> side of a parallel assignment if there is more than one lvalue, and the  
> resulting array is the value of the entire expression.
> 
> And I guess this wasn't a very good example:
> 
> x, y, z = \*[1, 2, 3]  
> puts x, y, z
> 
> --output:--  
> 1  
> 2  
> 3
> 
> Because you can eliminate the star and get the same output:
> 
> x, y, z = [1, 2, 3]  
> p x  
> p y  
> p z
> 
> --output:--  
> 1  
> 2  
> 3
> 
> But this is where the star makes a difference:
> 
> x, y, z = 1, [2, 3]  
> p x  
> p y  
> p z
> 
> --output:--  
> 1  
> [2, 3]  
> nil
> 
> x, y, z = 1, \*[2, 3]  
> p x  
> p y  
> p z
> 
> --output:--  
> 1  
> 2  
> 3
> 
> (This applies to ruby 1.8. See David Black's post for 1.9 behavior.)  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Max\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Max\_Williams](https://rubytalk.org/u/Max_Williams)\
**Post date:** [23 July 2009 11:38 UTC](https://rubytalk.org/t/ruby-star-whats-it/54555/12 "2009-07-23T11:38:06Z")

</div>

ah, cool, thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
