[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how do you do a hashed array of a class?

santogold

5/12/2006 1:32:00 AM

Warning: Ruby newbie post

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

I generally "get" Ruby but my Pickaxe book, nutshell book and
hours of web searching have left me clueless. I do have a half baked
metaprogramming solution to this but I think there has to be an easier
way than that.

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.

Thanks,
Tom

2 Answers

Dave Burt

5/12/2006 3:51:00 AM

0

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

Robert Klemme

5/12/2006 7:27:00 AM

0

Dave Burt wrote:
> 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

I'm not sure whether this is sufficient. OP didn't mention what data he
wants to store. Could be that all trades have to be recorded and max
and min found dynamically.

Also, using a sorted data structure might be a good idea because when
dates are used as keys then often range queries are used also (max in
the range from 2005-01-01 to 2005-06-01 etc.). These are made efficient
with an ordered data structure.

Some pointers

http://raa.ruby-lang.org/project/rub...
http://raa.ruby-lang.org/project/ru...

Kind regards

robert