From: dblack@... Date: 2006-01-12T23:09:01+09:00 Subject: Re: Newbie question about multiple assignments Hi -- On Thu, 12 Jan 2006, David Vallner wrote: > On Thu, 12 Jan 2006 13:39:00 +0100, Michael Ulm > 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. >> >> HTH, >> >> Michael >> >> > > > 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