From: Gavin Kistner Date: 2005-06-13T01:27:07+09:00 Subject: Re: Only if the object exists --Apple-Mail-4-699972693 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed On Jun 12, 2005, at 9:40 AM, Robert Klemme wrote: > Why change the parser if there are ways to do this that don't =20 > require such heavy language modifications? I suppose because it seems to me that the only reason that: tmp.bar if tmp=3Dfoo fails to work is a slight naivet=E9 by the parser. I expected it to =20 work, as there is no semantic difference between that and the code: if tmp=3Dfoo tmp.bar end That it does not work is consistent, however, with the odd fact that =20 introduction of a line like: tmp =3D x if false may affect the later scope of 'tmp'. I guess that's the real =20 disconnect for me. I'm coming from JavaScript, where the scoping for =20 a variable is not based on a top-down parsing, but is based on =20 whether the 'var' keyword declares the variable as local ANYWHERE in =20 the function. It strikes me as odd that this code: def foo 10.times{ i =3D i ? i+1 : 1 } puts i =3D i ? i+1 : 1 10.times{ i =3D i ? i+1 : 1 } puts i =3D i ? i+1 : 1 end results in "1" and "12" being written, instead of "11" and "22". Ruby is the first language I've seen where the scope of a variable is =20= not the same throughout the same block. If this is on purpose and/or =20 really cool and useful in situations I haven't considered, then I'll =20 accept "tmp.bar if tmp=3Dfoo" not working as a necessary casualty. But =20= it seems to me that the variable variable scope may be due to an =20 implementation flaw in the pre-processing of code, not a conscious =20 design decision. I'm not even proposing the 'massive' undertaking of scanning the =20 entire method for "tmp\s*=3D" outside of any blocks to predetermine if =20= tmp should be local (though IMO that should be done). I'm simply =20 suggesting that the parser conceptually invert the order of single-=20 line "yyyy if/unless xxxx" to process xxxx first. > class Dummy > def method_missing(*a,&b) self end > end > DUMMY =3D Dummy.new > > (foo || DUMMY).bar Interesting! > Apart from that I'd go with the "and" variant: > > tmp =3D foo and tmp.bar My old self who likes to use boolean guards as inline conditionals =20 would do that. My new self who wants to make code very readable and =20 clear to the casual observer is trying to use the words 'if' and =20 'unless'. (YMMV, but IMHO using booleans as conditionals is cool but =20 non-obvious. I know I've had multiple C++ coders look at my =20 JavaScript code using that syntax and say "...wtf is this? What does =20 that do?") --Apple-Mail-4-699972693--