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 `’
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 `’
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
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.
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 S. wrote:
should be allowed. “Path of least surprise” and all that, plus this
looks cleaner:x, y, z = 2, 4, 6
x, y += zI 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.
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
On 5/4/2010 5:31 AM, Kurtis Rainbolt-greene wrote:
x, y, z = 2, 4, 6
x, y += z
than
x, y, z = 2, 4, 6
x += z
y += z
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.
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” }
On Tue, 4 May 2010, Aldric G. wrote:
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.
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 Wed, 5 May 2010, Brian C. wrote:
[“x”, “y”].each { |v| eval “#{v} += z” }
Good point.
I should not reply to mailing lists when I'm distracted by a coding problem in another language.Back to 'bash’ing away…
Matt
On Wed, 5 May 2010, Matthew K. Williams wrote:
Local variables are not objects, and you can’t get “references” to them.
Back to 'bash’ing away…
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
Matt