From: Robert Klemme Date: 2009-08-24T19:58:30+09:00 Subject: Re: Stupid OpenStruct question 2009/8/24 Ralf Mueller : > Robert Klemme wrote: >> 2009/8/24 Ralf Mueller : >>> Robert Klemme wrote: >>>> On 24.08.2009 06:02, Terry Smith wrote: >>>>>> Great - ok how do I WORK with these once I have them? >>>> >>>> Just a remark: if your processing is mainly bases on keys this way then >>>> a >>>> Hash might the better data structure.  IMHO an OpenStruct is best used >>>> when >>>> you are lazy or do not know all the fields that you are going to use >>>> beforehand.  I find this especially useful during development, you can >>>> stuff >>>> in more and more fields but maybe later change it to a regular class (or >>>> Struct generated class). >>> I had a similar experience with using OpenStruct and Hashes and in the >>> end, >>> I derived my class from OpenStruct (build up a hash within the contructor >>> and create object with 'super()'). >> >> I am not sure what you are trying to convey here.  Why would I want to >> create a class that inherits OpenStruct and build up a Hash >> internally? > > The io-part of my app reads text files and creates objects from them > (csv-like files with named columns). > I use many of the Hash methods internaly, but the OpenStruct gives me all > the accessors for the api and the possibility to create objects from other > input types (eg. hashs or arrays, coming from a database access) with the > same constructor. You are aware that OpenStruct can be constructed with a Hash argument. If "intern" and "extern" are also separate phases you could work with Hashes internally and convert them to OpenStruct instances when handing off to external code. > Your Struct-example seems more elegant to me. I guess, I should start a > rewrite... :-) You'll be interested in method #members: irb(main):001:0> Struct.new(:foo,:bar).new.members => [:foo, :bar] Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/