Robert Klemme
1/11/2006 2:54:00 PM
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]
^
from (irb):38
from â?¥:0
>> Array.new = [1,2,3]
NoMethodError: undefined method `new=' for Array:Class
from (irb):39
from â?¥:0
>> (Array.new) = [1,2,3]
SyntaxError: compile error
(irb):40: syntax error
(Array.new) = [1,2,3]
^
from (irb):40
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
robert