# On parallel assignment

**URL:** <https://rubytalk.org/t/on-parallel-assignment/24029>\
**Category:** ruby-talk\
**Created:** [11 January 2006 14:29 UTC](https://rubytalk.org/t/on-parallel-assignment/24029 "2006-01-11T14:29:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christer\_Nilsson](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@Christer\_Nilsson](https://rubytalk.org/u/Christer_Nilsson)\
**Post date:** [11 January 2006 14:29 UTC](https://rubytalk.org/t/on-parallel-assignment/24029/1 "2006-01-11T14:29:52Z")

</div>

The parallel assignment syntax  
[a,b] = [c,d] is not allowed

I have to write  
a, b = [c,d]

Going deeper  
[a, [b, c]] = [1, [2, 3]] is not allowed

I have to write  
a, (b, c) = [1, [2, 3]]

I'm sure there is a good reason these constructs are disallowed.  
Can somebody enlighten me?

Christer

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

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [11 January 2006 14:58 UTC](https://rubytalk.org/t/on-parallel-assignment/24029/2 "2006-01-11T14:58:04Z")

</div>

Christer Nilsson wrote:

> The parallel assignment syntax  
> [a,b] = [c,d] is not allowed

The left side is an array constructor. In other words: take value of a  
and value of b and create a two element array. There is no assignment  
here. You cannot create an array on the left side of an assignment and  
try to assign to it:

> > =[1,2]

SyntaxError: compile error  
(irb):38: syntax error  
=[1,2]  
&nbsp;&nbsp;&nbsp;^  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):38  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from ❤:0

> > Array.new = [1,2,3]

NoMethodError: undefined method `new=' for Array:Class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):39  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from ❤:0

> > (Array.new) = [1,2,3]

SyntaxError: compile error  
(irb):40: syntax error  
(Array.new) = [1,2,3]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):40  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from ❤:0

> I have to write  
> a, b = [c,d]

I prefer

> > a,b=1,2

=\> [1, 2]

> > a

=\> 1

> > b

=\> 2

> Going deeper  
> [a, [b, c]] = [1, [2, 3]] is not allowed
> 
> I have to write  
> a, (b, c) = [1, [2, 3]]

You can as well do

> > a,(b,c)=1,[2,3]

=\> [1, [2, 3]]

> > a

=\> 1

> > b

=\> 2

> > c

=\> 3

The outermost array is implicit but you must create nested arrays  
explicitely.

> I'm sure there is a good reason these constructs are disallowed.  
> Can somebody enlighten me?

Hope so...

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [11 January 2006 15:36 UTC](https://rubytalk.org/t/on-parallel-assignment/24029/3 "2006-01-11T15:36:51Z")

</div>

Christer Nilsson wrote:

> The parallel assignment syntax  
> [a,b] = [c,d] is not allowed
> 
> I have to write  
> a, b = [c,d]
> 
> Going deeper  
> [a, [b, c]] = [1, [2, 3]] is not allowed
> 
> I have to write  
> a, (b, c) = [1, [2, 3]]
> 
> I'm sure there is a good reason these constructs are disallowed.  
> Can somebody enlighten me?
> 
> Christer

You should check out one of the websites that list upcoming syntax changes in Ruby 2.0 - parallel assignment is undergoing relatively significant changes to weed out possible ambiguity when it's used.

As well as nested parallel assignment is concerned, I'd say avoid - it's potentially a very confusing construct, and makes your intent unclear. Arrays usually imply the storing of values of homogenous type and purpose - when used in parallel assignment, it means you're most probably not using them that way, and should probably use a different data structure to store the information to make your code easier to understand.

David Vallner
