On Fri, 5 Dec 2003, Hal Fulton wrote:

> Date: Fri, 5 Dec 2003 07:13:04 +0900
> From: Hal Fulton <hal9000 / hypermetrics.com>
> Newsgroups: comp.lang.ruby
> Subject: Generalized break?
> 
> I hate to bring up possible language changes, since there is
> so much of that going around.
> 
> Matz, if this is stupid, just tell me so.
> 
> 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.

catch/throw is pretty general:

this works:

  code = <<-code
    if true
      def method
        42
      end
      if not false
        if true
          loop do
            p method
            throw :bail if rand < 0.5
          end
        end
      end
    end
  code


  def nesting code
    eval code
  end

  catch :bail do
    nesting code
  end


> 2. I have a mild dislike for the idiom if $0 == __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 != __FILE__

agreed.

how about something like

__TEST__

for this?

> 
> 
> 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.
> 
> There's no proper way to do this kind of thing, as far as I can
> see.
> 
> Thoughts?
> 
> Hal
> 
> 
> 
> 

-a

-- 

ATTN: please update your address books with address below!

===============================================================================
| EMAIL   :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE   :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP     :: http://www.ngdc.noaa.gov/stp/
| NGDC    :: http://www.ngdc.noaa.gov/
| NESDIS  :: http://www.nesdis.noaa.gov/
| NOAA    :: http://www.noaa.gov/
| US DOC  :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.  
| Art is everything else.  
|   -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done' 
===============================================================================