2009/8/24 Ralf M. [email protected]:
Robert K. 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
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
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, Struct, Enumerable, Object, Kernel, BasicObject]
irb(main):004:0> nm = NameValue.new “length”, 123
=> #<struct NameValue name=“length”, value=123>