Skip to content

Instantly share code, notes, and snippets.

@erubboli
Created August 29, 2010 13:00
Show Gist options
  • Save erubboli/556264 to your computer and use it in GitHub Desktop.
Save erubboli/556264 to your computer and use it in GitHub Desktop.
require "observer"
class Currency
include Observable
def initialize(symbol,rate)
@rate = rate
@symbol = symbol
end
def rate=(new_rate)
changed if new_rate != @rate
@rate = new_rate
notify_observers(@symbol,@rate)
end
end
class Graph
def initialize(currencies)
currencies.each do |currency|
currency.add_observer(self)
end
end
def update(symbol, rate)
puts "new rate for #{symbol} : #{rate}"
#.. update the graph
end
end
eur = Currency.new('EUR',1.2768)
usd = Currency.new('USD',1)
graph = Graph.new [eur,usd]
eur.rate = 1.2767 #=> new rate for EUR : 1.2767
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment