From: Joel VanderWerf Date: 2006-10-21T06:57:17+09:00 Subject: Re: [ANN] Dependency 1.0.0 Tim Fletcher wrote: > A Smalltalk-inspired dependency mechanism for Ruby. Like this: > > class StockWatcher > include Dependency::Methods > def initialize(stock) > stock.express_interest_in :price, :for => self, :send_back => > :price_changed > end > def price_changed(old_price, stock) > # gets called whenever the stock object changes it's price > end > end > > Completely unrelated to Rails' dependency code - similar to observers > though. > > http://rubyforge.org/projects/dependency > > gem install dependency > You can do this with http://raa.ruby-lang.org/project/observable: require 'observable' include Observable include Observable::Match class Stock extend Observable observable :price def initialize price @price = price end def sell puts "selling at #{price}" end end class StockWatcher GREATER_THAN = proc do |price| MatchProc.new { |test_price| test_price > price } end def initialize(stock) stock.when_price CHANGES do |price, old_price| puts "price = #{price} (was #{old_price.inspect})" end stock.when_price GREATER_THAN[20] do stock.sell end end end acme = Stock.new(10) watcher = StockWatcher.new(acme) 10.step(30, 5) do |acme.price| puts "---" end __END__ Output: price = 10 (was nil) --- price = 15 (was 10) --- price = 20 (was 15) --- selling at 25 price = 25 (was 20) --- selling at 30 price = 30 (was 25) --- -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407