From: Mike Harris Date: 2007-01-13T05:31:11+09:00 Subject: Re: Rake, Hoe, Meta-Programming, and Warnings (oh my!) Tim Pease wrote: > Is there a way to disable specific warnings from within a Ruby program > (not from the command line)? > > The warnings are coming from some meta-programming code that is > redefining instance methods on the fly based on user input. So maybe a > better question would be, is there a way to redefine methods without > causing Ruby to spew out a warning each time the method is redefined. > > This is the result of using Hoe (thanks Ryan, Eric, and other sun > deprived Rubyists) to run my unit tests. Hoe adds the -w flag to the > Ruby interpreter when it runs tests. This is a good thing as it found > several minor flaws in my code. But redefining methods is at the heart > of what I'm trying to do. > > > Example: > > class Pattern > def self.create_date_format_methods( pf ) > module_eval <<-CODE > def pf.format_date > Time.now.strftime "#{pf.date_pattern}" > end > CODE > end > end > > When running unit tests with the -w flag ... > > (eval):1: warning: redefine format_date > > > > Any thoughts on how to suppress the warning and/or change my > meta-programming so the warning does not show up? > > Blessings, > TwP > > 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