Created
January 30, 2013 14:30
-
-
Save hoelzro/4673636 to your computer and use it in GitHub Desktop.
Adding callbacks in Ruby
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
class Client | |
def initialize | |
@callbacks = {} | |
set_key 'q', method(:quit) | |
end | |
def set_key(key, callback) | |
@callbacks[key] = callback | |
end | |
def quit | |
throw :quit_loopy | |
end | |
def loopy | |
catch :quit_loopy do | |
loop do | |
c = getc | |
callback = @callbacks[c] | |
next if callback.nil? | |
callback.call | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment