Created
July 1, 2013 16:46
-
-
Save robpe/5902503 to your computer and use it in GitHub Desktop.
Ruby goto (PoC)
This file contains 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
module Kernel | |
require 'singleton' | |
require 'continuation' | |
class Env < Hash | |
include Singleton | |
def initialize | |
super | |
end | |
end | |
def label(name) | |
callcc { |cc| Env.instance[name] = cc } | |
end | |
def goto(name) | |
raise "Goto couldn't find label: #{name}" unless Env.instance.key?(name) | |
Env.instance[name].call | |
end | |
end |
This file contains 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
require './goto' | |
def goop(times, &blk) | |
label :loop | |
blk.call | |
goto :loop unless (times-=1).zero? | |
end | |
goop(10) { | |
puts 'Hi goto!' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment