From: Jon Egil Strand Date: 2007-02-02T18:31:00+09:00 Subject: Re: Performance hit of define_method > it's not so much the fact that you are using define_method, which is a bit > slow, but the fact that one Staticperson you define the methods on once while > in Dynamicperson you define the methods each and every time even though the > def simply clobbers the existing one. But of course, thank you. To keep the dynamicity of Dynamicperson, which is the functionality I'm trying to achieve, I made the define_method conditional: class Dynamicperson def initialize(personalia) @personalia = personalia @personalia .each_key do |k| self.class.send(:define_method, k) { @personalia[k] } unless self.respond_to?(k) end end end Still, it would be better to do this only once, on class level rather than instance level class Dynamicperson_class def initialize(personalia) @personalia = personalia end def Dynamicperson_class.define_methods(personalia) personalia.each_key do |k| send(:define_method, k) { @personalia[k] } end end end Followed by _one_ call of peter = {:firstname => "Peter", :surname => "Pan", :country => "Neverland"} Dynamicperson_class.define_methods(peter) Then benchmarking: ------------------------------------------------------------------------ Static: 1.772000 0.050000 1.822000 ( 1.873000) Dynamic pr. instance: 27.260000 1.322000 28.582000 ( 28.861000) Dynamic pr. instance conditionally: 3.284000 0.060000 3.344000 ( 3.375000) Dynamic pr. class: 2.614000 0.040000 2.654000 ( 2.944000) -------------------------------------------------------------- total: 36.402000sec Thank's for pointing me in the right direction Ara. JE