Prev: * splat error
Next: Calculation in a block
From: Aldric Giacomoni on 4 May 2010 09:54 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/.
From: Walton Hoops on 4 May 2010 10:05 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 > 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.
From: Matthew K. Williams on 4 May 2010 10:39 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. 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
From: Brian Candler on 4 May 2010 11:09 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/.
From: Matthew K. Williams on 4 May 2010 11:20
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.... Matt |