File: tc.rb

Project: throw_catch -- Throw/Catch Lab

#!/usr/bin/env ruby

ThrowStack = []

def cc_catch(sym)
  callcc { |cc|
    ThrowStack << [sym, cc]
    begin
      result = yield
    ensure
      ThrowStack.pop
    end
    result
  }
end

def cc_throw(sym, value=nil)
  while ! ThrowStack.empty? && ThrowStack.last[0] != sym
    ThrowStack.pop
  end
  if ThrowStack.empty?
    fail NameError, "uncaught throw `#{sym}'"
  end
  cc = ThrowStack.last[1]
  ThrowStack.pop
  cc.call(value)
end


[ Index ][ Presentation ]
Generated by [ source2html ]