Skip to content

Instantly share code, notes, and snippets.

@rubydubee
Created December 3, 2011 07:44
Show Gist options
  • Save rubydubee/1426455 to your computer and use it in GitHub Desktop.
Save rubydubee/1426455 to your computer and use it in GitHub Desktop.
Observer and Duck type
$: << File.join(File.dirname(__FILE__), "/")
require "Observable.rb"
require "Observer1.rb"
require "Observer2.rb"
observable = Observable.new
o1 = Observer1.new(observable)
o2 = Observer2.new(observable)
observable.called
class Observable
def initialize
@observers = []
end
def observe(o)
@observers << o
end
def called
puts "Observable is being called"
@observers.each { |o| o.update }
end
end
class Observer1
def initialize(obs)
obs.observe(self)
end
def update
puts "Okay! Observing in Observer1"
end
end
class Observer2
def initialize(obs)
obs.observe(self)
end
def update
puts "Okay! Observing in Observer2"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment