Skip to content

Instantly share code, notes, and snippets.

@hoelzro
Created January 30, 2013 14:30
Show Gist options
  • Save hoelzro/4673636 to your computer and use it in GitHub Desktop.
Save hoelzro/4673636 to your computer and use it in GitHub Desktop.
Adding callbacks in Ruby
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