From: Robert Klemme Date: 2012-02-01T07:16:54+09:00 Subject: Re: Multiple assignment in conditional On Tue, Jan 31, 2012 at 7:46 AM, Gavin Sinclair wrote: > I find this a strange Ruby error. > > foo = [1,2] > > # The following is fine. > if (a = foo) >  puts a > end > > # The following causes a SyntaxError: multiple assignment in conditional. > if (a, b = foo) >  puts a > end > > > Why would that be a syntax error?  Surely the grammar is > >  if EXPR >    ... > > and (a, b = foo) is an expression, isn't it? Yeah, I'd agree. But why write such obfuscated code? > Just curious, though my curiosity was prompted by a failing piece of real > code. Well, there is no point in having assignments in conditionals - unless it's a loop in which case the code often becomes more elegant, e.g.: while ( line = gets ) puts line end For a simple if and unless there is really no advantage in having an assignment in the condition. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/