From: Robert Klemme Date: 2007-03-11T01:35:05+09:00 Subject: Re: OpenStruct,, know what keys are set On 10.03.2007 16:53, Aaron Smith wrote: > Thanks for all the input. > > Heres some testing: > > require 'ostruct' > require 'benchmark' > > lady = OpenStruct.new > 0.upto(100) do |i| > eval("lady.cat#{i} = i") > #puts eval("lady.cat#{i}") > end > > Benchmark.bm do |x| > x.report("Find all keys 1"){lady.methods(false).grep(/[^=]$/)} > x.report("Find all keys 2"){lady.instance_variable_get("@table").keys} > x.report("Find all keys 3"){lady.keys_added} > end > > #puts "\n" + lady.methods(false).grep(/[^=]$/).to_s > #puts "\n" + lady.instance_variable_get("@table").keys.to_s > #puts "\n" + lady.keys_added #this is always nil. > > > OUTPUT: > user system total real > Find all keys 1 1.130000 0.050000 1.180000 ( 1.185657) > Find all keys 2 0.020000 0.000000 0.020000 ( 0.019773) > Find all keys 3 0.000000 0.000000 0.000000 ( 0.000020) > > Results speak for themselves. Um, for what implementation of "keys_added"? The comment points out that it always returns nil - I suspect you just measured performance of an OpenStruct ad hoc getter: irb(main):005:0> OpenStruct.new.keys_added => nil irb(main):006:0> OpenStruct.new.foo_bar => nil Kind regards robert