Add a new gem: `mruby-catch`.

Implements `catch`/`throw` non-local jump inherited from Lisp.

`catch([tag]) {|tag| block }  -> obj`

Example:
```
  catch(:foo) { 123 }               # => 123
  catch(:foo) { throw(:foo, 456) }  # => 456
  catch(:foo) { throw(:foo) }       # => nil
```
parent 471479e7
MRuby::Gem::Specification.new('mruby-catch') do |spec|
spec.license = 'MIT'
spec.author = 'mruby developers'
spec.summary = 'Catch / Throw non-local Jump'
end
class ThrowCatchJump < Exception
def initialize(tag, val)
@tag = tag
@val = val
super("uncaught throw :#{tag}")
end
def _tag
@tag
end
def _val
@val
end
end
module Kernel
def catch(tag, &block)
block.call(tag)
rescue ThrowCatchJump => e
unless e._tag == tag
raise e
end
return e._val
end
def throw(tag, val=nil)
raise ThrowCatchJump.new(tag, val)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment