--0016e6de00ec235f8b04b7ed69da
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Feb 1, 2012 at 5:49 AM, Gavin Sinclair <gsinclair / gmail.com> wrote:

> On Tue, Jan 31, 2012 at 6:22 PM, Josh Cheek <josh.cheek / gmail.com> wrote:
>
> >> # The following causes a SyntaxError: multiple assignment in
> conditional.
> >> if (a, b  oo)
> >>  puts a
> >> end
>
> > What did you expect your real code to do? It seems to me that it should
> > raise an error unless it's an array of size 2
>
> Nope.  It can return anything at all and Ruby will deal with it.
> Parallel assignment is (thankfully) flexible.
>
> def foo; 1; end
> a, b  oo            # returns 1; a 1, b nil
>
> def bar; [1,2,3,4,5]; end
> a, b  ar            # returns [1,2,3,4,5]; a 1, b 2
>
> > and if it is, then it should
> > return an array of size two, making the conditional necessarily true.
>
> Nope.  The conditional can be false.
>
> def foo; nil; end
> a, b  oo            # returns nil; a nil, b nil
>
> > So I don't know what you were expecting to happen.
>
> def foo; nil; end
>
> if (a, b  oo)
>   puts "outcome 1"
> else
>  puts "outcome 2"   # I was expecting this.
> end
>
> Of course I don't have a function that returns nil unconditionally!
> Just showing the structure.
>
>
Sorry, but I find this incredibly opaque.

Could you just write what you expect the evaluation to be in each of these
cases?

a, b  , 1
a, b  , nil
a, b  il, 1
a, b  il, nil

I'd expect return values of [1, 1], [1, nil], [nil, 1], [nil, nil] which
are all true.

Now does this change when the RHS is an array instead of two values?
For example, would `a, b  1, 1]` return a different value than `a, b [1, 1]`?


Your example seems particularly devious in that it only has one value on
the RHS of the assignment. This means that the second argument will be
assigned nil (because there is no value for it), and because assignment
operators always return the RHS, it will then return whatever single value
was over there. This causes it to look like you are assigning multiple
values to the variables and having it return nil, when in fact you are only
assigning one value (the second variable is along for the ride and will
always be nil).

There are so many nuanced ruby edge cases one must know (and then must also
know the implementation of how it is being returned) to understand that
example that I think it has more overhead than it is worth.

--0016e6de00ec235f8b04b7ed69da--