On Mon, 5 Jan 2004 03:06:48 +0900, GGarramuno wrote:
> Thanks for mentioning that. That could indeed be some future
> gotcha I could run into.
> 
> Still, that is not dealing with the flow problem I have.
> 
> Testing it now with:
> 
> catch(:arg) do
>   while $stdin.getc
>     begin
>       p "A"
>       while 1
>         p "B"
>         throw :arg if 1
>       end
>     ensure
>         # do a lot of stuff here
>       p "ensure"
>     end
>   end
> end

As Jamis said, you want:

  while $stdin.getc
    begin
      catch(:arg) do
        p "A"
        while true
          p "B"
          throw :arg if true
        end
      end
    ensure
      p "ensure"
    end
  end

The catch block wraps what you want to be caught; in your original
code, you completely exit the loop.

Remember as well, that only nil and false are false in Ruby; that
means 0 is a true value.

> and I still exit the loop right away. I want to be able to do a
> "next" on the outer loop from within the inner loop and the
> throw/catch mechanism seems to be only a solution to breaking out
> of loops, not iterating to the next step (while evaluating the
> condition). I'm sure I am missing some obvious way of doing this.

As Guy pointed out, $stdin.getc returns an integer, not a character,
but you can get integer values with ?<char>: ?a, ?b, ?:, ?!, etc.

  catch(:outer) do
    while s = $stdin.getc
      print s.chr # Converts integer to character
      begin
        catch(:inner) do
          print "A"
          while true
            print "B"
            throw :inner if true
          end
        end
      ensure
        print "E"
      end
      throw :outer if s == ?q
    end
  end

-austin
--
austin ziegler  * austin / halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2004.01.04
                    * 14.40.58