Dave Burt
5/12/2006 3:51:00 AM
santogold@mn.rr.com wrote:
> I am trying to create an array of a class to accomplish something like
> the following:
>
> Class Loadstocks
> #load stock data into an array, crunch numbers and hash it, etc...
> end
>
>
> aapl = Loadstocks("aapl")
> msft = Loadstocks("msft")
>
> print aapl['2005-02-01].low
> print aapl['2005-02-01].high
> print msft['2005-02-01].high
> print msft['2005-02-01].close
>
> ...
>
> The part I am stuck on is making a hashed array of objects that I can
> deal with easily. (loading the data , etc is almost intuitive)
>
> Any code fragments or web links are greatly appreciated.
You can do this with a hash of structs:
irb> StockDay = Struct.new(:high, :low, :close)
=> StockDay
irb> def loadstocks() {'2005-02-01' => StockDay.new(4, 2, 3)} end
=> nil
irb> aapl = loadstocks
=> {"2005-02-01"=>#<struct StockDay high=4, low=2, close=3>}
irb> aapl['2005-02-01'].low
=> 2
Cheers,
Dave