From: Jon Leighton Date: 2011-01-26T03:09:05+09:00 Subject: Re: The finer points of postfix conditionals. Robert Klemme wrote in post #977336: > On Mon, Jan 24, 2011 at 7:18 PM, Jon Leighton > wrote: >> Thanks for the explanation. FWIW I think it is a shame that postfix >> conditionals are semantically different to normal ones, but it's good to >> know why the difference occurs. > > See Gary's reply. Yes, I agree that it's surprising this is done at parse time. But regardless of the cause and effect it is still surprising (to me) that the two conditional syntaxes have different semantics :) >> I started to wonder about this when I was using the following code: >> >> bla(foo) if foo = self.foo >> >> In other words, I was trying to put the value of the method foo into the >> local variable foo in order to avoid calling the method twice. To me the >> above is quite an elegant way of doing that, so it's a shame that it >> does not actually have the intended effect ;) [it all came crashing down >> when I renamed the foo variable] > > Frankly, I don't find this elegant at all. It's a matter of taste. I agree with you that in almost all situations that assignment during a conditional test is not nice. However, I liked this solution precisely because the local var was named exactly the same as the method. So the way I read it in my head was "call bla with foo, and oh btw, don't call the foo method twice". So in this situation the 'if foo = self.foo' was purely a performance tweak and nothing else. A side note, if you will - it didn't change the semantics of the statement. But as I say, it's a matter of taste. And it doesn't work anyway ;) Jon -- Posted via http://www.ruby-forum.com/.