From: Garthy D Date: 2012-01-31T16:57:17+09:00 Subject: Re: Multiple assignment in conditional Hi Gavin, On 31/01/12 17:16, 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? > > Just curious, though my curiosity was prompted by a failing piece of real > code. I can understand why the language rejects that construct (I've never tried it myself- an interesting find!). The method itself would be returning an array (correct?), and an array would always be treated as true in the expression, regardless of the results. Thus, it is likely to be some sort of error (why put an always-statically-true expression in a conditional?), and hence knocks it back with an error. I'm wondering as to the best behaviour if it were actually handled directly by the language (which I'm not specifically suggesting). True if any of the multiple assignments evaluate to true? Or just the first? Or last? Certainly not always-true. I can't say I'd know a sensible default. :/ Garth