Error getting “mango” from:
{“apple”=>“fruit”,
“orange”=>“fruit”,
“lettuce”=>“vegetable”,
“tomato”=>“depends_on_who_you_ask”}
0: Return not having found the value. (:continue)
1: Try getting the key from the hash again. (:try_again)
2: Use a new hash. (:use_new_hash)
3: Use a new key. (:use_new_key)
Choose number: 3
Enter a new key: “apple”
value: “fruit”
% ruby readmes/restarts.rb
readmes/restarts.rb:49:in `
Error getting “mango” from:
{“apple”=>“fruit”,
“orange”=>“fruit”,
“lettuce”=>“vegetable”,
“tomato”=>“depends_on_who_you_ask”}
0: Return not having found the value. (:continue)
1: Try getting the key from the hash again. (:try_again)
2: Use a new hash. (:use_new_hash)
3: Use a new key. (:use_new_key)
Choose number: 2
Enter a new hash: { “mango” => “mangoish fruit” }
value: “mangoish fruit”
Translated to Ruby from http://c2.com/cgi/wiki?LispRestartExample
== Technical Notes
Cond has been tested on MRI 1.8.6, 1.8.7, 1.9.1, 1.9.2, and
jruby-1.4.
Each thread keeps its own list of handlers, restarts, and other data.
All operations are fully thread-safe.
Except for the redefinition +raise+, Cond does not silently modify any
of the standard classes.
The essential implementation is small and simple: it consists of two
per-thread stacks of hashes (handlers and restarts) with merge-push
and pop operations.
== DSL Form and Raw Form
The optional require ‘cond/dsl’ defines some pseudo-keywords
in the global scope which comprise a DSL for the system. These
methods are also available with require ‘cond’ through the
Cond singleton (e.g. Cond.handling) or by including Cond::DSL into the
class or module which uses them.
The DSL shown in the above examples is a thin layer concealing the
underlying hashes. It is equivalent to the following raw form. You
are free to use either form according to preference or circumstance.
require ‘cond’
def divide(x, y)
restarts = {
:return_this_instead => lambda { |value|
throw :leave, value
}
}
catch :leave do
Cond.with_restarts restarts do
raise ZeroDivisionError if y == 0
x/y
end
end
end
handlers = {
ZeroDivisionError => lambda { |exception|
Cond.invoke_restart :return_this_instead, 42
}
}
Cond.with_handlers handlers do
puts divide(10, 2) # => 5
puts divide(18, 3) # => 6
puts divide(4, 0) # => 42
puts divide(7, 0) # => 42
end
== Limitations
There must be a call to +raise+ inside Ruby code (as opposed to C
code) in order for a handler to be invoked.
The above synopsis gives an example: Why is there a check for division
by zero when +ZeroDivisionError+ would be raised anyway? Because
Fixnum#/ is written in C.
It is still possible for handlers to intercept these raises, but it
requires redefining a wrapped version of the method in question:
Cond.wrap_instance_method(Fixnum, 😕)
Once this has been called, the line
raise ZeroDivisionError if y == 0
is unnecessary.
It is possible remove this limitation by modifying the Ruby
interpreter to call Kernel#raise dynamically.
== Links
- Documentation: http://cond.rubyforge.org
- Rubyforge home: http://rubyforge.org/projects/cond/
- Download: http://rubyforge.org/frs/?group_id=7916
- Repository: http://github.com/quix/cond/tree/master
== Author
- James M. Lawrence < quixoticsycophant@gmail.com >
== License
Copyright © 2009 James M. Lawrence. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
‘Software’), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.