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

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?

irb(main):001:0> a = b = x = y = 0
=> 0
irb(main):002:0> a,b = x,y = 1,2
=> [0, 1, 2]

Doesn't work without seeing previous assignments to the variables
involved. Maybe working as designed? Don't know.

Jim

Email address is _invalid_! Read here; please respond here.

···

On Fri, 4 Nov 2005 06:59:31 +0900, Ron M <rm_rails@cheapcomplexdevices.com> wrote:

This

   a,b = x,y = 1,2

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

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 :slight_smile: 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 :slight_smile:

David

···

On Fri, 4 Nov 2005, Ron M wrote:

--
David A. Black
dblack@wobblini.net