On Tue, Jan 31, 2012 at 7:46 AM, Gavin Sinclair <gsinclair / gmail.com> wrote=
:
> I find this a strange Ruby error.
>
> foo =3D [1,2]
>
> # The following is fine.
> if (a =3D foo)
> =A0puts a
> end
>
> # The following causes a SyntaxError: multiple assignment in conditional.
> if (a, b =3D foo)
> =A0puts a
> end
>
>
> Why would that be a syntax error? =A0Surely the grammar is
>
> =A0if EXPR
> =A0 =A0...
>
> and (a, b =3D 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 =3D 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/