If you have a lot of readings then Struct is the winner.

If you have equal read/write then Hash

And forget for OpenStruct

Here is why and how I benchmark all those methods



guda: ~/vanilla_ui/vanilla_properties  (v2 *$ u=) ∴ irb
ruby-1.8.7-p330 :001 > require ‘benchmark’
=> true
ruby-1.8.7-p330 :002 > require ‘ostruct’
=> false
ruby-1.8.7-p330 :003 > n = 5000000
=> 5000000
ruby-1.8.7-p330 :004 >
ruby-1.8.7-p330 :005 >   puts “convertions”
convertions
=> nil
ruby-1.8.7-p330 :006 >
ruby-1.8.7-p330 :007 >   Benchmark.bm do |x|
ruby-1.8.7-p330 :008 >      x.report(“Struct init at start”) { CustomerOne = Struct.new(:name, :age);  s = CustomerOne.new(‘a’);   n.times do s.name; end }
ruby-1.8.7-p330 :009?>    x.report(“Struct with write op”) { CustomerTwo = Struct.new(:name, :age);  s = CustomerTwo.new;  s.name = ‘a’; n.times do s.name; end }
ruby-1.8.7-p330 :010?>    x.report(“Hash”) {  s = Hash.new;  s[:name] = ‘a’; n.times do   ; s[:name]; end }
ruby-1.8.7-p330 :011?>    x.report(“OpenStruct”) { s = OpenStruct.new;  s.name = ‘a’; n.times do   ;  s.name; end }
ruby-1.8.7-p330 :012?>   end
user     system      total        real
Struct init at start
1.160000   0.000000   1.160000 (  1.155457)
Struct with write op  1.200000   0.000000   1.200000 (  1.208754)
Hash  1.510000   0.000000   1.510000 (  1.507588)
OpenStruct  5.990000   0.000000   5.990000 (  5.999714)
=> true
Lets do some = operations
ruby-1.8.7-p330 :015 >   Benchmark.bm do |x|
ruby-1.8.7-p330 :016 >      x.report(“Struct init at start”) { CustomerMale = Struct.new(:name, :age);  n.times do s = CustomerMale.new(‘a’);  s.name; end }
ruby-1.8.7-p330 :017?>    x.report(“Hash”) { n.times do   ; s = Hash.new;  s[:name] = ‘a’; s[:name]; end }
ruby-1.8.7-p330 :018?>    x.report(“Struct with write op”) { CustomerFemale = Struct.new(:name, :age);  n.times do s = CustomerFemale.new;  s.name = ‘a’; s.name; end }
ruby-1.8.7-p330 :019?>    x.report(“OpenStruct”) { n.times do   ; s = OpenStruct.new;  s.name = ‘a’; s.name; end }
ruby-1.8.7-p330 :020?>   end
user     system      total        real
Struct init at start  9.930000   0.000000   9.930000 (  9.943670)
Hash 11.090000   0.000000  11.090000 ( 11.127766)
Struct with write op 12.230000   0.000000  12.230000 ( 12.243452)
OpenStruct still working ….