2010-04-14
■ [ruby] Common Lisp的な、「再開可能な例外」をRubyで
実装は以下。もちろん継続ですw
例:
10.times do |i| begin raise "OH NO!" puts "OH YES! #{i}" i += 1 rescue => err if i < 5 err.continue ### これ(Exception#continue)が「例外が発生した次の行」に飛ぶ else raise err end end end
実行結果:
/tmp % ruby-1.9 -rcontinuation a.rb OH YES! 0 OH YES! 1 OH YES! 2 OH YES! 3 OH YES! 4 a.rb:48:in `rescue in block in <main>': RuntimeError (RuntimeError) from a.rb:40:in `block in <main>' from a.rb:39:in `times' from a.rb:39:in `<main>'