From: Robert Klemme Date: 2009-08-24T18:07:18+09:00 Subject: Re: Stupid OpenStruct question 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? Basically this is what OpenStruct does already: irb(main):012:0> o = OpenStruct.new => # irb(main):013:0> o.instance_variables => [:@table] irb(main):014:0> o.instance_variable_get("@table").class => Hash If you need to much access via Hash methods chances are that you probably rather want to use a Hash in the first place. That's what I meant. > But what do you exactly mean by 'Struct > generated class'? Is there a smart way of creating abstract classes (without > deriving or explicit coding) from Structs? Struct.new creates new classes: irb(main):001:0> NameValue = Struct.new :name, :value => NameValue irb(main):002:0> NameValue.class => Class irb(main):003:0> NameValue.ancestors => [NameValue, Struct, Enumerable, Object, Kernel, BasicObject] irb(main):004:0> nm = NameValue.new "length", 123 => # irb(main):005:0> nm.class => NameValue Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/