Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Forked from anonymous/gist:997625
Created May 29, 2011 10:35
Show Gist options
  • Save Mon-Ouie/997636 to your computer and use it in GitHub Desktop.
Save Mon-Ouie/997636 to your computer and use it in GitHub Desktop.
# Observer
# ----------------
module Observable
def add_observer(observer)
observers.push(observer)
end
def remove_observer(observer)
observers.delete(observer)
end
def notify_observers(event, options)
observers.each do |observer|
observer.notify(event, options)
end
end
def observers
@observers ||= []
end
end
class Foo
include Observable
def sup
self.notify_observers('sup', { :message => 'Wassup' })
end
end
class Bar
def notify(event, options)
puts "Bar.notify: #{event}, #{options}"
end
end
f = Foo.new
b = Bar.new
f.add_observer(b)
f.sup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment