Created
April 20, 2012 17:22
-
-
Save dasch/2430415 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Raises a condition. If a handler is set up, it can restart the condition | |
# and return to this place. | |
# | |
# name - The Symbol name of the condition. | |
# | |
# Returns nothing if the condition was restarted, and doesn't return at all | |
# otherwise. | |
def condition(name) | |
callcc do |cont| | |
throw name, cont | |
end | |
end | |
# Handles a condition, possibly restarting it. | |
# | |
# name - The Symbol name of the condition. | |
# options - A Hash of options: | |
# :restart - true if the condition should be restarted, false otherwise. | |
# | |
# Returns nothing. | |
def handle(name, options = {}) | |
cont = catch(name) do | |
yield | |
# If no conditions were raised, this will return from the method. | |
return | |
end | |
# We'll only reach these lines if a condition was raised. If :restart => true | |
# was passed, we resume the place where #condition was called. Otherwise, we | |
# just ignore it. We could possibly: | |
# | |
# - Raise an exception. | |
# - Do some logging. | |
# - Perform a use-specific restart, e.g. passing a directive to the raise site, | |
# such as "use this value", "skip the item", etc. | |
if options[:restart] | |
cont.call | |
end | |
end | |
handle :deprecation, :restart => false do | |
puts "A" | |
condition :deprecation | |
puts "B" | |
end | |
# Outputs: | |
# | |
# A | |
handle :deprecation, :restart => true do | |
puts "A" | |
condition :deprecation | |
puts "B" | |
end | |
# Outputs: | |
# | |
# A | |
# B |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$LABELS = {} | |
def label(name) | |
callcc do |cont| | |
$LABELS[name] = cont | |
end | |
end | |
def goto(name) | |
$LABELS[name].call | |
end | |
i = 0 | |
label :loop | |
puts "LOOPIN'!" | |
i += 1 | |
goto :loop if i < 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment