Forum: Ruby Newbie question about multiple assignments

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A0b6bf4901811894ffbb1bfc3ddb47ef?d=identicon&s=25 Alex (Guest)
on 2006-01-12 11:24
irb(main):091:0> a=1
=> 1
irb(main):092:0> b=2
=> 2
irb(main):093:0> a,b=b,a
=> [2, 1]
irb(main):094:0> a=1
=> 1
irb(main):095:0> b=2
=> 2
irb(main):096:0> a, b = b, a = a, b
=> [2, 1, 2]                           <-- Why?

What is the order for the assignments
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-12 12:07
(Received via mailing list)
To your why, another why: why do you need to use multiple parallel
assignments in one statement? That said, parallel assignments are
somewhat
known to be a rather messy bit of syntax I'm apparently not as savvy
with
as I thought, since I can't make heads or tails of what is really
happening.

My personal tip woukd be that the assignments are associative
right-to-left, and only the leftmost assignment is evaluated as a
parallel
one.

What probably happens is broken down into simpler statements:

a = a, b # a == [1, 2]
a, b = b, *a # a ==2, b==1

where the return value of the expression is the rvalue.

So the end values are what you expect, just the return value of the
expression isn't.

Hint: it's easy to avoid obscure syntax, so do that if possible. Noone
really admires godawful one-liners from hell in code he expects to
understand a week after writing it, even if there is a certain charm to
getting things done with them ;)

Also, parallel assignment syntax is undergoing certain subtle, but
significant changes that seem to resolve some of the ambiguity - you
might
want to look on one of the webpages that summarize changes in Ruby 1.9.

David Vallner
F91f1034d620825594db83db92ca1711?d=identicon&s=25 Michael Ulm (Guest)
on 2006-01-12 13:41
(Received via mailing list)
Alex wrote:
> irb(main):096:0> a, b = b, a = a, b
> => [2, 1, 2]                           <-- Why?
>
> What is the order for the assignments
>

Keep in mind, that

a, b = c, d

is just shorthand for

a, b = [c, d]

So, your expression

a, b = b, a = a, b

gets parsed as

a, b = [b, a = a, b]

and thus produces the observed behaviour.

HTH,

Michael
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-12 13:51
(Received via mailing list)
On Thu, 12 Jan 2006 13:39:00 +0100, Michael Ulm
<michael.ulm@isis-papyrus.com> wrote:

>> => 2
>
> and thus produces the observed behaviour.
>
> HTH,
>
> Michael
>
>


That doesn't seem to explain the return value [2, 1, 2] of the
expression.

David Vallner
3dfbc15ef3489522b304bcbc3aa88cf2?d=identicon&s=25 Karl Allmark (Guest)
on 2006-01-12 14:07
(Received via mailing list)
>>> => 2
>>
>> and thus produces the observed behaviour.
>David Vallner
Hi List

Just getting up to speed with ruby myself, try reading Davids
explaination
as:

a, b = [b,(a = a), b]

Karl Allmark
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-12 14:16
(Received via mailing list)
On Thu, 12 Jan 2006 14:06:56 +0100, Karl Allmark
<karl.allmark@citytechnology.com> wrote:

>
>
>

Not quite, what I had in mind would be this one-liner:

a, b = [b, *[a = [a, b]]]

I have a slight completely unfounded doubt the interpreter would
implicitly associate (a = a) in the middle of the expression.

The results observed in irb ARE however ambiguous with respect to both
of
those possible interpretations.

David Vallner
F91f1034d620825594db83db92ca1711?d=identicon&s=25 Michael Ulm (Guest)
on 2006-01-12 14:48
(Received via mailing list)
David Vallner wrote:

>>> => [2, 1]
>>
>> gets parsed as
>>
>> a, b = [b, a = a, b]
>>
>> and thus produces the observed behaviour.
>>
>
>
> That doesn't seem to explain the return value [2, 1, 2] of the expression.
>

Why not? the return value of an assignment is the right hand
side. In this case [2, 1 , 2]. The first and last value there
coming from b, and the middle value is the return value of the
assignment a = a.

Try this:

irb(main):001:0> a, b = [1, 2, 3, 4] # sets a=1 and b=2
=> [1, 2, 3, 4]

irb(main):002:0> a, b = [1, c = 2, 3] # sets a=1, b=2, and c = 2
=> [1, 2, 3]

HTH,

Michael
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-12 15:10
(Received via mailing list)
Hi --

On Thu, 12 Jan 2006, David Vallner wrote:

>>> irb(main):094:0> a=1
>> a, b = c, d
>>
>
> That doesn't seem to explain the return value [2, 1, 2] of the expression.

An assignment expression returns its right-hand side.  In this case,
that's [b, a = a, b]:

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


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
A0b6bf4901811894ffbb1bfc3ddb47ef?d=identicon&s=25 Alex (Guest)
on 2006-01-12 15:15
thanks guys i figured it out
I was trying to see how this things works in ruby just for fun
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-12 21:09
(Received via mailing list)
On Thu, 12 Jan 2006 15:09:01 +0100, <dblack@wobblini.net> wrote:

>
Ah well, for some strange reason I thought the parser would check for a
parallel assignment form inside the array constructor as well.
Apparently
not... Right, that example pretty much clears the issue up.

David Vallner
30a69d3e9ddb45dda39df00ca36a2eeb?d=identicon&s=25 Andrew McGuinness (Guest)
on 2006-01-12 23:14
(Received via mailing list)
David Vallner wrote:
>
>
> Ah well, for some strange reason I thought the parser would check for a
> parallel assignment form inside the array constructor as well.
> Apparently  not... Right, that example pretty much clears the issue up.
>
Within method arguments, the "," gets parsed as an argument separator
not an array separator:

irb(main):094:0> puts( a,b = 1,2 )
1
1
2
=> nil
irb(main):095:0> puts( ( a,b = 1,2 ) )
1
2
=> nil



irb(main):103:0> a,b=1,2
=> [1, 2]
irb(main):104:0> a,b=(b,a=a,b)
=> [1, 2]
irb(main):105:0> a
=> 1
irb(main):106:0> b
=> 2
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-13 15:34
(Received via mailing list)
Hi --

On Fri, 13 Jan 2006, David Vallner wrote:

>>
>> David
>>
>
>
> Ah well, for some strange reason I thought the parser would check for a
> parallel assignment form inside the array constructor as well. Apparently
> not... Right, that example pretty much clears the issue up.

The thing is, if you've got an array [a,b], and you add to it so that
it is [a,b,c=1], the meaning of the "a,b" part doesn't change; rather,
you're understood to have added an element.  The comma semantics are
different in these different contexts, in other words.


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
This topic is locked and can not be replied to.