# Array assignment with multiple args

**URL:** <https://www.ruby-forum.com/t/array-assignment-with-multiple-args/137629>\
**Category:** Ruby\
**Created:** [May 14, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/array-assignment-with-multiple-args/137629 "2008-05-14T21:20:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@jhorner](https://www.ruby-forum.com/u/jhorner)\
**Post date:** [May 14, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/array-assignment-with-multiple-args/137629/1 "2008-05-14T21:20:35Z")

</div>

Hi all. I’m just starting to learn Ruby and have hit a problem that is  
being extremely difficult to find information on. When given a line of  
code such as:

a = “this”, b = “that”, c = “those”

Is there a concise and elegant way of describing why variable a is  
assigned as an array, but variable b is not? There must be a simple  
explanation as to ‘why’ this is happening. The only description of what  
is going on here that I have found is that this is interpreted as

a = “this”, (b = “that”), (c = “those”)

and not

a = “this”, (b = “that”, c = “those”)

and that the arguments are read from right to left. This hasn’t really  
helped my understanding, as that much can be deduced just by putting the  
line through the Ruby interpreter, and I don’t see how reading the  
assignments from right to left assigns [“this”,“that”,“those”] to  
variable a but only “that” to variable b.

Thank you for taking the time to ponder this.  
Jeff H.

---

<div class="post-metadata">

**Author:** ![jhorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@jhorner](https://www.ruby-forum.com/u/jhorner)\
**Post date:** [May 14, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/array-assignment-with-multiple-args/137629/2 "2008-05-14T21:29:57Z")

</div>

On May 14, 2008, at 1:20 PM, Jeff H. wrote:

> The only description of what  
> is going on here that I have found is that this is interpreted as
> 
> a = “this”, (b = “that”), (c = “those”)
> 
> and not
> 
> a = “this”, (b = “that”, c = “those”)
> 
> and that the arguments are read from right to left.

start reading left to right, say ‘a equals everything to the right of  
the equal sign’, now interpret the right side… continue

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![jhorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@jhorner](https://www.ruby-forum.com/u/jhorner)\
**Post date:** [May 14, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/array-assignment-with-multiple-args/137629/3 "2008-05-14T21:33:27Z")

</div>

On May 14, 2008, at 3:20 PM, Jeff H. wrote:

> Hi all. I’m just starting to learn Ruby and have hit a problem that is  
> being extremely difficult to find information on. When given a line of  
> code such as:
> 
> a = “this”, b = “that”, c = “those”

This statement is parsed as:

a = [“this”, (b = “that”), (c = “those”)]

The right hand side of the assignment to variable ‘a’  
is an array of three items:  
first item: a string with value “this”  
second item: a string with value “that”  
third item: a string with value “those”

As a side effect, the variable b is assigned to “that”  
and the variable c is assigned to “those”

The example you gave is pretty unusual. I _think_ you  
were thinking about parallel assignment, which would  
look like:

a, b, c = “this”, “that”, “those”

Gary W.

---

<div class="post-metadata">

**Author:** ![jhorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@jhorner](https://www.ruby-forum.com/u/jhorner)\
**Post date:** [May 14, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/array-assignment-with-multiple-args/137629/4 "2008-05-14T22:29:53Z")

</div>

I know about parallel assignment, but the oddity of this statement’s  
behavior has had me stumped. I also know to interpret this statement as  
an assignee, assignor and assignment. I would like to be able to explain  
‘why’ the other assignments are allowed; and, since they are, ‘why’ b is  
not assigned [“that”,“those”]. I know this must sound like a 3 year  
old’s question (eg: but, why? ad infinitum) 😛

---

<div class="post-metadata">

**Author:** ![jhorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@jhorner](https://www.ruby-forum.com/u/jhorner)\
**Post date:** [May 15, 2008, 12:27am UTC](https://www.ruby-forum.com/t/array-assignment-with-multiple-args/137629/5 "2008-05-15T00:27:41Z")

</div>

Jeffrey H. wrote:

> When given a line of code such as:
> 
> a = “this”, b = “that”, c = “those”
> 
> […]  
> have found is that this is interpreted as
> 
> a = “this”, (b = “that”), (c = “those”)
> 
> and not
> 
> a = “this”, (b = “that”, c = “those”)
> 
> I would like to be able to explain ‘why’ the other assignments are allowed

The code of your program is parsed, by a “parser”. The parser has rules  
about what can be found in a program, and if you look at these rules  
you’ll discover why your code doesn’t tranlstae to ’ a = “this”, (b =  
“that”, c = “those”) '.

The rules can be found as BNF notation:

[http://docs.huihoo.com/ruby/ruby-man-1.4/yacc.html](http://docs.huihoo.com/ruby/ruby-man-1.4/yacc.html)

(that doc it’s probably quite obsolete, but it can serve our purpose.)

You can see that multiple assignment is described using this pattern:

MLHS `=’ MRHS

It stands for “Multiple-assiggment Left Hand Side = Multiple-assignment  
Right Hand Side”.

Now, this expression is part of a larger structure: EXPR (it stand  
probbaly for “expression”).

Now, let’s look at the pattern for MRHS:

ARGS [`,' `\*’ ARG]

Now, let’s look at the pattern for ARGS:

ARG (`,’ ARG)\*

It’s composed of ARG thingies, so let’s look at the pattern for ARG:

ARG : LHS `=' ARG | LHS OP_ASGN ARG | ARG `…’ ARG  
| ARG `...' ARG | ARG `+’ ARG  
| ARG `-' ARG | ARG `\*’ ARG  
[lot’s of lines snipped]

Now comes the important part: you’ll discover that there’s a thing ARG  
_can’t_ be: EXPR. And since the MLHS=MRHS is under EXPR, it follows that  
the right hand side or multiple assignment can’t (directly) contain a  
multiple asignment.

In other words, ’ a = “this”, b = “that”, c = “those” ’ can’t be parsed  
into ’ a = “this”, (b = “that”, c = “those”) ’ because this isn’t a  
valid structure.

The BNF I used my not be the actual one used for ruby, but that’s not  
important, I just wanted to show why a parser chooses one interpretation  
for a source-code and not another.

==

BTW, you can see to what tree Ruby parses your source by installing the  
‘rubynode’ gem. Then you’d be able to do:

require ‘rubygems’  
require ‘rubynode’  
pp ’ a = “this”, b = “that”, c = “those” '.parse\_to\_nodes.transform

---

<div class="post-metadata">

**Author:** ![jhorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@jhorner](https://www.ruby-forum.com/u/jhorner)\
**Post date:** [May 15, 2008, 7:57am UTC](https://www.ruby-forum.com/t/array-assignment-with-multiple-args/137629/6 "2008-05-15T07:57:11Z")

</div>

Hi,

In message “Re: Array assignment with multiple args”  
on Thu, 15 May 2008 04:20:35 +0900, Jeff H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Hi all. I’m just starting to learn Ruby and have hit a problem that is  
|being extremely difficult to find information on. When given a line of  
|code such as:  
|  
|a = “this”, b = “that”, c = “those”  
|  
|Is there a concise and elegant way of describing why variable a is  
|assigned as an array, but variable b is not? There must be a simple  
|explanation as to ‘why’ this is happening. The only description of what  
|is going on here that I have found is that this is interpreted as  
|  
|a = “this”, (b = “that”), (c = “those”)

More precisely,

a = (“this”, (b = “that”), (c = “those”))

But I am not sure what you wanted to accomplish by

a = “this”, b = “that”, c = “those”

In Ruby, commas are not expression separator.

|and not  
|  
|a = “this”, (b = “that”, c = “those”)  
|  
|and that the arguments are read from right to left. This hasn’t really  
|helped my understanding, as that much can be deduced just by putting the  
|line through the Ruby interpreter, and I don’t see how reading the  
|assignments from right to left assigns [“this”,“that”,“those”] to  
|variable a but only “that” to variable b.

Multiple assignment does not nest. Assignments in right hand side is  
considered as normal assignments.

```
          matz.

```
