Something I expected to work, but didn't!

  • Thread starter Kurtis Rainbolt-greene
  • Start date
K

Kurtis Rainbolt-greene

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>'


:(
 
A

Alex DeCaria

Kurtis said:
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>'


:(

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
 
A

Alex DeCaria

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.
 
K

Kurtis Rainbolt-greene

Alex said:
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
 
G

Gavin Sinclair

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
 
A

Aldric Giacomoni

Gavin said:
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.
 
W

Walton Hoops

Alex DeCaria wrote:


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
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.
 
M

Matthew K. Williams

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
 
B

Brian Candler

Matthew said:
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" }
 
M

Matthew K. Williams

Matthew said:
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....

Matt
 
M

Matthew K. Williams

Matthew said:
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....

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
 

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
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top