From: Gavin Kistner Date: 2005-06-12T00:45:56+09:00 Subject: Only if the object exists --Apple-Mail-1-611104768 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed If I write code like this: self.foo.bar if self.foo then ruby runs the #foo method twice. (Javascript is the same way.) If #foo is a lengthy process which I know has no side-effects, this is wasteful. But, I need to ensure that #foo didn't return nil. I have (at least) three choices: if ( tmp=foo ) puts tmp.bar end ( tmp=foo ) && ( puts tmp.bar ) puts tmp.bar if ( tmp=foo ) ...except that the last doesn't work, if 'tmp' has not been previously seen as a local variable. Unfortunately, that's the form that I really think looks best. Would it be possible to have the parser recognize the single-line 'if/ unless' case and process the right side before the left side (the order in which the code is executed)? -- "When I am working on a problem I never think about beauty. I only think about how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." - R. Buckminster Fuller --Apple-Mail-1-611104768--