how do you do a hashed array of a class?

Discussion in 'Ruby' started by santogold@mn.rr.com, May 12, 2006.

  1. Guest

    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
     
    , May 12, 2006
    #1
    1. Advertising

  2. Dave Burt Guest

    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
     
    Dave Burt, May 12, 2006
    #2
    1. Advertising

  3. Dave Burt wrote:
    > 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
     
    Robert Klemme, May 12, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel
    Replies:
    1
    Views:
    1,054
    Kevin Collins
    Aug 14, 2003
  2. Matt Breedlove
    Replies:
    1
    Views:
    427
    Eduard W. Lohmann
    Nov 24, 2003
  3. Tommy

    Is Cookies hashed by default

    Tommy, Aug 6, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    363
    Tommy
    Aug 6, 2003
  4. =?Utf-8?B?QmlsbCBCb3Jn?=

    Convert hashed password to 128-bit binary

    =?Utf-8?B?QmlsbCBCb3Jn?=, Apr 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    5,390
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Apr 22, 2005
  5. Ian
    Replies:
    3
    Views:
    171
Loading...

Share This Page