Skip to content

Instantly share code, notes, and snippets.

@penso
Created September 26, 2014 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save penso/863c1f6ab2ea6af864a4 to your computer and use it in GitHub Desktop.
Save penso/863c1f6ab2ea6af864a4 to your computer and use it in GitHub Desktop.
class Foo
def initialize
@callbacks = {}
end
def on(type, &block)
@callbacks[type] = block
end
def callback(type, *args)
return unless @callbacks[type]
@callbacks[type].call(*args)
end
end
foo = Foo.new
foo.on(:success) do
puts "succeeded"
end
foo.on(:failed) do |error|
puts "Error: #{error}"
end
foo.on(:message) do |from, msg|
puts "from: #{from} msg: #{msg}"
end
foo.callback(:success)
foo.callback(:failed, "mon error", "mon error")
foo.callback(:message, "fabien", "mon message")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment