how do you do a hashed array of a class?

S

santogold

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
 
D

Dave Burt

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
 
R

Robert Klemme

Dave said:
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/ruby-bsearch/
http://raa.ruby-lang.org/project/ruby-rbtree/

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top