Module: Kernel

Defined in:
lib/restarts.rb

Instance Method Summary collapse

Instance Method Details

#raise_with_restarts(condition) {|restart| ... } ⇒ Object

Similar to the standard Kernel#raise command. Allows you to throw exceptions The benefit is though that using raise_with_restarts adds the infrastructure so that the rescue code can tell the exception to restart and recover from a specific point.

See README.txt for example

Yields:

  • (restart)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/restarts.rb', line 17

def raise_with_restarts(condition)
  restart = callcc do |cc|
    # Have the continuation object accessible via the #restart method.
    # FIXME: check for pre defined methods by the name "restart".
    (class <<condition; self; end).class_eval do
      define_method(:restart) do |*id|
        if id.empty?
          return cc
        else
          return cc.call(id[0])
        end
      end
    end

    raise condition
  end

  # This allows raise_condition to be used in the same way as regular raise
  # Just don't give an exception.
  #
  # IDEA: maybe alias this to default Kernal#raise, would be nice.
  yield restart if block_given?
end