Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how do you do a hashed array of a class?

Reply
Thread Tools

how do you do a hashed array of a class?

 
 
santogold@mn.rr.com
Guest
Posts: n/a
 
      05-12-2006
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

 
Reply With Quote
 
 
 
 
Dave Burt
Guest
Posts: n/a
 
      05-12-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      05-12-2006
Dave Burt wrote:
> (E-Mail Removed) 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/ruby-bsearch/
http://raa.ruby-lang.org/project/ruby-rbtree/

Kind regards

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Advice on converting hashed packages to pseudo-hashed packages Ian Perl Misc 3 02-12-2005 12:17 AM
Importing 80+ hashed and 1 array into several perl scripts Matt Breedlove Perl 1 11-24-2003 09:47 PM
hashed array in array need the keys... and length Daniel Perl 1 08-14-2003 06:49 PM
Is Cookies hashed by default Tommy ASP .Net 0 08-06-2003 06:26 PM



Advertisments