Created
September 26, 2014 14:34
-
-
Save penso/863c1f6ab2ea6af864a4 to your computer and use it in GitHub Desktop.
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 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