Should "a,b = x,y = 1,2" give an error?

R

Ron M

This

a,b = x,y = 1,2

give me a
"NameError: undefined local variable or method `x' for main:Object",
yet

a = x = 1 ; b = y = 2;

or

a,b = (x,y = 1,2)

seem to work fine. It doesn't really bother me since the
workarounds are easy, but the first one looks cleaner to me.
Would this still be an error in Ruby2?
 
D

David A. Black

Hi --

This

a,b = x,y = 1,2

give me a
"NameError: undefined local variable or method `x' for main:Object",
yet

a = x = 1 ; b = y = 2;

or

a,b = (x,y = 1,2)

seem to work fine. It doesn't really bother me since the
workarounds are easy, but the first one looks cleaner to me.

It's a bit over-cleaned in this case :) It's parsed as:

(a,b) = x,(y=1),2
Would this still be an error in Ruby2?

Matz has discussed possibly changing multiple assignment, but I think
in a case like this the rules would still be the same. That's
actually good -- we want y=1 to mean y=1 :)


David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,806
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top