Something I expected to work, but didn't!

irb(main):001:0> x = 2
=> 2
irb(main):002:0> y = 4
=> 4
irb(main):003:0> x, y *= 2
SyntaxError: (irb):3: syntax error, unexpected tOP_ASGN, expecting '='
x, y *= 2
       ^
  from /usr/local/bin/irb:12:in `<main>'

:frowning:

···

--
Posted via http://www.ruby-forum.com/.

Kurtis Rainbolt-greene wrote:

irb(main):001:0> x = 2
=> 2
irb(main):002:0> y = 4
=> 4
irb(main):003:0> x, y *= 2
SyntaxError: (irb):3: syntax error, unexpected tOP_ASGN, expecting '='
x, y *= 2
       ^
  from /usr/local/bin/irb:12:in `<main>'

:frowning:

You need the same number of arguments on the right hand side as you do
on the left. For example,

x, y = 3

print x, y => 3, nil

x, y = 3, 3

print x, y => 3, 3

···

--
Posted via http://www.ruby-forum.com/\.

You need the same number of arguments on the right hand side as you do
on the left. For example,

x, y = 3

print x, y => 3, nil

x, y = 3, 3

print x, y => 3, 3

And, compound assignment operators such as *=, +=, etc. aren't allowed
with parallel assignment.

···

--
Posted via http://www.ruby-forum.com/\.

Alex DeCaria wrote:

compound assignment operators such as *=, +=, etc. aren't allowed
with parallel assignment.

Ah-ha, someone gets what I was talking about, but my point was they
should be allowed. "Path of least surprise" and all that, plus this
looks cleaner:

x, y, z = 2, 4, 6

x, y += z

than

x, y, z = 2, 4, 6

x += z
y += z

···

--
Posted via http://www.ruby-forum.com/\.

should be allowed. "Path of least surprise" and all that, plus this
looks cleaner:

x, y, z = 2, 4, 6

x, y += z

I have no idea what that code snippet is intended to do! So it
doesn't look cleaner to me.

Gavin

I think you misunderstand how multiple assignment works:
irb(main):001:0> x, y=2
=> 2
irb(main):002:0> x
=> 2
irb(main):003:0> y
=> nil

In other words, even if that was allowed, the two snippets you provided
would not be equivalent.

···

On 5/4/2010 5:31 AM, Kurtis Rainbolt-greene wrote:

Alex DeCaria wrote:

compound assignment operators such as *=, +=, etc. aren't allowed
with parallel assignment.
    

Ah-ha, someone gets what I was talking about, but my point was they
should be allowed. "Path of least surprise" and all that, plus this
looks cleaner:

x, y, z = 2, 4, 6

x, y += z

than

x, y, z = 2, 4, 6

x += z
y += z

Gavin Sinclair wrote:

should be allowed. "Path of least surprise" and all that, plus this
looks cleaner:

x, y, z = 2, 4, 6
x, y += z

I have no idea what that code snippet is intended to do! So it
doesn't look cleaner to me.

Yes.. I'm not sure this follows the POLS principle. It probably follows
the Path Of Least Keyboard Access principle, but POLKA should remain a
dance.

···

--
Posted via http://www.ruby-forum.com/\.

I'm not sure why you would think that the POLS is for the operator on the right to be applied to both of the objects. The way that x,y = foo,bar works is that what is on the right side is considered as an array, and the elements are assigned to the variables on the left. It only works on assignment, not any random operator.

Now, you could say:
[x, y].each {|v| v += z}

But x,y += z doesn't seem obvious to me.

YMMV, of course.

Matt

···

On Tue, 4 May 2010, Aldric Giacomoni wrote:

Gavin Sinclair wrote:

should be allowed. "Path of least surprise" and all that, plus this
looks cleaner:

x, y, z = 2, 4, 6
x, y += z

I have no idea what that code snippet is intended to do! So it
doesn't look cleaner to me.

Yes.. I'm not sure this follows the POLS principle. It probably follows
the Path Of Least Keyboard Access principle, but POLKA should remain a
dance.

Matthew K. Williams wrote:

Now, you could say:
[x, y].each {|v| v += z}

Except that does nothing except calculate values and throw them away
(since v drops out of scope at the end of the block). x and y are
unchanged.

Local variables are not objects, and you can't get "references" to them.
You'd have to do something awful with eval to get the effect you want:

["x", "y"].each { |v| eval "#{v} += z" }

···

--
Posted via http://www.ruby-forum.com/\.

Good point.

<MANTRA>
I should not reply to mailing lists when I'm distracted by a coding problem in another language.
</MANTRA>

Back to 'bash'ing away....

Matt

···

On Wed, 5 May 2010, Brian Candler wrote:

Matthew K. Williams wrote:

Now, you could say:
[x, y].each {|v| v += z}

Except that does nothing except calculate values and throw them away
(since v drops out of scope at the end of the block). x and y are
unchanged.

Local variables are not objects, and you can't get "references" to them.
You'd have to do something awful with eval to get the effect you want:

["x", "y"].each { |v| eval "#{v} += z" }

I just had a semi-evil thought, however.....

x,y = ([x,y].map{|v| v+=z})

At that point, however,

x+=z
y+=z

are much clearer :wink:

Matt

···

On Wed, 5 May 2010, Matthew K. Williams wrote:

On Wed, 5 May 2010, Brian Candler wrote:

Matthew K. Williams wrote:

Now, you could say:
[x, y].each {|v| v += z}

Except that does nothing except calculate values and throw them away
(since v drops out of scope at the end of the block). x and y are
unchanged.

Local variables are not objects, and you can't get "references" to them.
You'd have to do something awful with eval to get the effect you want:

["x", "y"].each { |v| eval "#{v} += z" }

Good point.

<MANTRA>
I should not reply to mailing lists when I'm distracted by a coding problem in another language.
</MANTRA>

Back to 'bash'ing away....