Skip to content

Instantly share code, notes, and snippets.

@WA9ACE
Created June 2, 2014 17:00
Show Gist options
  • Save WA9ACE/57341304bb38032dc438 to your computer and use it in GitHub Desktop.
Save WA9ACE/57341304bb38032dc438 to your computer and use it in GitHub Desktop.
Event Library Tutorial
module EventMapper
def on(event, &block)
if @events.nil?
@events = { event => [block] }
elsif @events[event].nil?
@events[event] = [block]
else
@events[event] << block
end
end
def fire(event, *args)
if args.empty?
@events[event].each do |proc|
Thread.new { proc.call }
end
else
@events[event].each do |proc|
Thread.new { proc.call *args }
end
end
end
end
class Thing
include EventMapper
end
t = Thing.new
t.on :test do
puts "testing"
end
t.on :test_with_params do |thing1, thing2|
puts "I was passed #{thing1}, and #{thing2}"
end
t.fire :test
t.fire :test_with_params, "Hallo", "hooman"
sleep 0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment