From: Robert Klemme Date: 2012-02-02T16:47:44+09:00 Subject: Re: Multiple assignment in conditional On Thu, Feb 2, 2012 at 3:56 AM, botp wrote: > On Thu, Feb 2, 2012 at 4:15 AM, Joel VanderWerf > wrote: >> You can trick ruby: >> >> def foo; nil; end >> >> if (_ = (a, b = foo)) >> >>  puts "outcome 1" >> else >>  puts "outcome 2" >> end >> >> So, the question is parsing, not semantics. > > > indeed. ruby just checks the last assignment. > >> if (x,y=(a, b = foo)) >> p 1 >> end > SyntaxError: (irb):17: multiple assignment in conditional > > but i do not know why it has to do that checking. too much work. and > could probably slow down ruby. Probably because (a,b=foo) in all but the latest version always yields an Array which is true hence does not make sense as a condition. You could write if (true) as well. 08:45:50 ~$ allruby x.rb CYGWIN_NT-5.1 padrklemme2 1.7.9(0.237/5/3) 2011-03-29 10:10 i686 Cygwin ======================================== ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] [nil] "true" ======================================== ruby 1.9.3p0 (2011-10-30) [i386-cygwin] nil ======================================== jruby 1.6.3 (ruby-1.8.7-p330) (2011-07-07 965162f) (Java HotSpot(TM) Client VM 1.7.0_02) [Windows XP-x86-java] [nil] "true" ======================================== jruby 1.6.3 (ruby-1.9.2-p136) (2011-07-07 965162f) (Java HotSpot(TM) Client VM 1.7.0_02) [Windows XP-x86-java] [nil] "true" ======================================== 08:47:11 ~$ cat -n x.rb 1 2 def foo;nil end 3 4 x = (a,b=foo) 5 p x 6 7 if (x = (a,b=foo)) 8 p "true" 9 end 08:47:15 ~$ Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/