Michael Ulm
1/12/2006 1:46:00 PM
David Vallner wrote:
> On Thu, 12 Jan 2006 13:39:00 +0100, Michael Ulm
> <michael.ulm@isis-papyrus.com> wrote:
>
>> Alex wrote:
>>
>>> 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
>>>
>>
>> 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.
>>
>
>
> 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
--
Michael Ulm
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: michael.ulm@isis-papyrus.com
Visit our Website: www.isis-papyrus.com