From: Wesley J Landaker Date: 2003-12-05T07:40:11+09:00 Subject: Re: Generalized break? --Boundary-02=_If7z/zAyb7lYFir Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Description: signed data Content-Disposition: inline On Thursday 04 December 2003 3:13 pm, Hal Fulton wrote: > I've sometimes wished for a "break" that would work in (almost) any > context -- one that would do "the sensible thing" instead of just > giving an error saying you can't do a break here. > > Two examples are: > > 1. A long string, possibly multiline/multi-statement, is being > evaled. I want the capability to abort the eval. > > 2. I have a mild dislike for the idiom if $0 =3D=3D __FILE__ (lines) end > -- because I always indent inside an if, and I don't want to indent, > and I really just want to say: break if $0 !=3D __FILE__ I actually would like that too, especially for #2, since it avoids lots=20 of nasty nesting for unit tests and such. > > In both these contexts, I initially tried exit. Of course, that > doesn't work -- it exits the entire program. A "return" seems like > the next choice, but that only makes sense within a method. I think break makes sense here, and I'd use it if it were available. > There's no proper way to do this kind of thing, as far as I can > see. Well, maybe not proper, but here is *a* way to do it. ;) #!/usr/bin/ruby puts "File loaded." if $0 =3D=3D __FILE__ then puts <<-EOF ; end # look, no indent, and we're not nested in an if! =3D) puts "File called directly." EOF =2D-=20 Wesley J. Landaker - wjl@icecavern.net OpenPGP FP: 4135 2A3B 4726 ACC5 9094 0097 F0A9 8A4C 4CD6 E3D2 --Boundary-02=_If7z/zAyb7lYFir Content-Type: application/pgp-signature Content-Description: signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.3 (GNU/Linux) iD8DBQA/z7fH8KmKTEzW49IRAiw7AJkB1uBHqAQdHPfeePGZcE5/FvyjigCcDLv3 SOkwsuP4LccDfRZrDlhi5b8= =25q1 -----END PGP SIGNATURE----- --Boundary-02=_If7z/zAyb7lYFir--