From: Ralf Mueller Date: 2009-08-24T17:06:08+09:00 Subject: Re: Stupid OpenStruct question Robert Klemme wrote: > On 24.08.2009 06:02, Terry Smith wrote: >> got it >> >> ff = s.fields >> >> ff.each{|g| >> puts s.instance_eval g >> } >> 1 >> 2 >> >> Terry Smith wrote: >>> Ok - I know how to get the fields of an Openstruct object like so >>> >>> class OpenStruct >>> def fields >>> @table.keys.map{|k| k.to_s} >>> end >>> end >>> >>> a = OpenStruc.new() >>> a.foo = 1 >>> a.bar = 2 >>> >>> a.fields -> ["foo", "bar"] >>> >>> 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). > Hi Robert, 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()'). 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? regards ralf