[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ANN] Dependency 1.0.0

Tim Fletcher

10/20/2006 9:07:00 PM

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/...

gem install dependency

2 Answers

Joel VanderWerf

10/20/2006 9:57:00 PM

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/...
>
> gem install dependency
>

You can do this with http://raa.ruby-lang.org/project/...:

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

Tim Fletcher

11/3/2006 10:53:00 AM

0

Joel VanderWerf wrote:
> Tim Fletcher wrote:
> > A Smalltalk-inspired dependency mechanism for Ruby.
>
> You can do this with http://raa.ruby-lang.org/project/...:

And observer.rb :)

http://ruby-doc.org/stdlib/libdoc/observer/rdoc/...