Skip to content

Instantly share code, notes, and snippets.

@JAndritsch
Created February 21, 2014 19:12
Show Gist options
  • Save JAndritsch/9141200 to your computer and use it in GitHub Desktop.
Save JAndritsch/9141200 to your computer and use it in GitHub Desktop.
class EventBus
@@events = {}
def self.register(name, callback)
@@events[name] ||= []
@@events[name] << callback
end
def self.publish(name, *args)
return if @@events[name].nil?
@@events[name].each do |callback|
callback.call(*args)
end
end
end
# register
EventBus.register("testing", Proc.new { |a| puts "testing was executed once: #{a}" })
EventBus.register("testing", Proc.new { |a, b| puts "testing was executed again: #{a}, #{b}" })
# publish
EventBus.publish("testing", "arg 1", "arg2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment