From: Tim Pease Date: 2007-01-13T05:54:43+09:00 Subject: Re: Rake, Hoe, Meta-Programming, and Warnings (oh my!) On 1/12/07, Mike Harris wrote: > I understand that this isn't identical to what you're doing, but it > demonstrates how define_method won't cause a warning. > > class Foo > define_method(:bar) do > puts "bar" > end > def cat > puts "cat" > end > end > > Foo.new.bar > Foo.new.cat > > class Foo > define_method(:bar) do > puts "bar2" > end > def cat > puts "cat2" > end > end > > Foo.new.bar > Foo.new.cat > > >ruby -w foo.rb > bar > cat > foo.rb:17: warning: method redefined; discarding old cat > bar2 > cat2 > > > There is going to be a performance hit with define_method. Since the method body is a block, each time it is called a Binding to the instance will have to be created in which the block will be run. $ cat a.rb require 'benchmark' class Object def method() end define_method(:method1) {} end this_many = 1_000_000 ::Benchmark.bm(10) do |bm| bm.report('method :') {this_many.times {method()}} bm.report('method1 :') {this_many.times {method1()}} end $ ruby a.rb user system total real method : 0.400000 0.000000 0.400000 ( 0.400000) method1 : 1.272000 0.000000 1.272000 ( 1.282000) It is a factor of 3 slower :( Blessings, TwP