From: Robert Klemme Date: 2005-06-13T16:15:31+09:00 Subject: Re: Only if the object exists Douglas Livingstone wrote: > On 6/12/05, Mark Hubbart wrote: >> IMHO, it wouldn't be a "heavy language modification" (irrespective of >> the work required). I've always felt uncomfortable with the fact that >> an assignment in a statement modifier doesn't show up for the >> statement it is modifying, even though it is executed first. I feel >> the following two examples should be equivalent: >> >> if t = foo >> t.bar >> end >> >> -- and: >> >> t.bar if t = foo >> > > Hmm, using a Ruby 1.8.x: > > irb(main):001:0> class Foo > irb(main):002:1> def name > irb(main):003:2> 'foo' > irb(main):004:2> end > irb(main):005:1> end > => nil > irb(main):006:0> foo = Foo.new > => # > irb(main):007:0> bar.name if bar = foo > NameError: undefined local variable or method `bar' for main:Object > from (irb):7 > irb(main):008:0> bar = nil > => nil > irb(main):009:0> bar.name if bar = foo > => "foo" > > Seems like the same "reading ahead of itself" problem of trying to do > the whiteout quiz on a file without perfect syntax. Example: > > irb(main):012:0> 89u33@$T$#$b if false > SyntaxError: compile error > (irb):12: syntax error > 89u33@$T$#$b if false > ^ > from (irb):12 > > Should you get a syntax error on code never called? Yes, of course! Syntax analysis comes before execution. Every programming language (whether compiled or interpreted) needs at least syntactic correct input. And btw: don't trust IRB when it comes to local variables! Kind regards robert