From: Aaron Smith Date: 2007-03-11T01:54:30+09:00 Subject: Re: OpenStruct,, know what keys are set > 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 Ah yes, you caught me. I completely overlooked this code in the above exmaple: class OpenStruct def keys_added @table.keys.map{|k| k.to_s} end end -- New Tests: require 'ostruct' require 'benchmark' class OpenStruct def keys_added @table.keys.map{|k| k.to_s} end end lady = OpenStruct.new 0.upto(10) 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} x.report("Find all keys 4"){lady.instance_variable_get("@table").keys.map{|k| k.to_s }} end #puts "\n" + lady.methods(false).grep(/[^=]$/).to_s #puts "\n" + lady.instance_variable_get("@table").keys.to_s #puts "\n" + lady.keys_added.to_s #puts "\n" + lady.instance_variable_get("@table").keys.map{|k| k.to_s }.to_s OUTPUT: user system total real Find all keys 1 0.000000 0.000000 0.000000 ( 0.000077) Find all keys 2 0.000000 0.000000 0.000000 ( 0.000014) Find all keys 3 0.000000 0.000000 0.000000 ( 0.000017) Find all keys 4 0.000000 0.000000 0.000000 ( 0.000016) -- Posted via http://www.ruby-forum.com/.