> literals, you would, of course, use the equivalent way, but if your data
> comes in as nested arrays, then you can't assign like that, instead you
> have
> to do something like this:
>
> def parallel(values)
>   x, (y, (z, a))=values
>   [x,y,z,a]
> end
> def alternative(values)
>   x       = values.shift
>   values  = values.shift
>   y       = values.shift
>   values  = values.shift
>   z       = values.shift
>   a       = values.shift
>   [x,y,z,a]
> end
> parallel    [1, [2, [3, 4]]] # => [1, 2, 3, 4]
> alternative [1, [2, [3, 4]]] # => [1, 2, 3, 4]
>
> Now, I don't normally store data like that, so I haven't ever done
> anything
> quite that fancy, but I use arrays on the RHS on occasion. It might look
> something like this (though I don't normally store my data like this,
> either
> -- it's really hard to think of a decent example!).

Thank you for sharing the use case. In this case, it helps. But like you
said, it seems like an answer in search of a question.

>
> $stdin = DATA
> while input = gets
>   name , num = input.split
>   puts "#{name.capitalize}'s favourite number is #{num}"
> end
> ...
>
> The alternative of
> name , num = input.split
> is
> values = input.split
> name = values.shift
> num = values
>
> I consider the former to be much more elegant as it avoids a temporary
> variable.
This however is already covered by me as the first case (with HIGH
elegance rating) since you are expecting the line to contain exactly two
strings (and if it is not so, it's an exceptional situation) and hence
the (expected) number of lvalues = number of rvalues. I do
like/understand such application of parallel assignment.

Thanks, again!

-- 
Posted via http://www.ruby-forum.com/.