Issue #10450 has been updated by Alex Boyd.


Multiple assignment returns the object on the right hand side, *before* `to_ary` is called on it:

~~~
irb(main):006:0> class A; def to_ary; [1, 2]; end; end
=> :to_ary
irb(main):007:0> a, b = A.new
=> #<A:0x007fe273da6260>
~~~

It will therefore, as bug hit mentions, return `nil` if the right hand side is `nil`.

But even wrapping in parentheses doesn't work (ruby -v: ruby 2.2.0dev (2014-12-08 trunk 48728) [x86_64-linux]):

~~~
irb(main):001:0> if (a, b = nil) then 'yes' else 'no' end
SyntaxError: (irb):1: multiple assignment in conditional
	from bin/irb:11:in `<main>'
~~~

This patch:

~~~
--- parse.y
+++ parse.y
@@ -9432,10 +9432,6 @@ static int
 assign_in_cond(struct parser_params *parser, NODE *node)
 {
     switch (nd_type(node)) {
-      case NODE_MASGN:
-       yyerror("multiple assignment in conditional");
-       return 1;
-
       case NODE_LASGN:
       case NODE_DASGN:
       case NODE_DASGN_CURR:
~~~

seems to work (but, of course, still requires parentheses):

~~~
irb(main):004:0> if (a, b = nil) then 'yes' else 'no' end
=> "no"
irb(main):005:0> if (a, b = [1, 2]) then 'yes' else 'no' end
=> "yes"
~~~

I don't myself have any huge interest in being able to do this, but now I'm curious: why was this prohibited in the first place? (There could very well be some unexpected consequence of allowing this that I've overlooked...)

----------------------------------------
Bug #10450: multiple assignment in conditional
https://bugs.ruby-lang.org/issues/10450#change-50332

* Author: bug hit
* Status: Rejected
* Priority: Normal
* Assignee: 
* Category: 
* Target version: 
* ruby -v: ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
multiple assignment is an expression whose value can be truthy (array) or falsy (nil, false), so why is there a restriction on its use in conditionals?  A warning perhaps is justified, but a syntax error, why?


irb(main):001:0> if a, b = nil then 1 else 0 end
SyntaxError: (irb):1: syntax error, unexpected ',', expecting keyword_then or ';' or '\n'
if a, b = nil then 1 else 0 end
     ^
(irb):1: syntax error, unexpected keyword_then, expecting end-of-input
if a, b = nil then 1 else 0 end
                  ^
	from /home/alex/.rbenv/versions/2.1.4/bin/irb:11:in `<main>'
irb(main):002:0> if (a, b = nil) then 1 else 0 end
SyntaxError: (irb):2: multiple assignment in conditional
	from /home/alex/.rbenv/versions/2.1.4/bin/irb:11:in `<main>'
irb(main):003:0> (a, b = nil) ? 1 : 0
SyntaxError: (irb):3: multiple assignment in conditional
	from /home/alex/.rbenv/versions/2.1.4/bin/irb:11:in `<main>'
irb(main):004:0> (a, b = nil)
=> nil




-- 
https://bugs.ruby-lang.org/