From: Daniel Berger Date: 2007-04-06T02:20:04+09:00 Subject: Re: Redefining initialize while staying -w clean On Apr 5, 10:49 am, "Daniel Berger" wrote: > On Apr 5, 10:30 am, James Edward Gray II > wrote: > > > > > > > On Apr 5, 2007, at 11:20 AM, Daniel Berger wrote: > > > > On Apr 5, 10:06 am, Andrew Johnson wrote: > > >> Daniel Berger wrote: > > > >>> That doesn't appear to work. Do you have a code snippet where it > > >>> does > > >>> work? Perhaps I've missed something. > > > >> Perhaps I've misunderstood your case -- the following runs -w > > >> clean on > > >> Ruby 1.8.6 > > > >> $ cat init.rb > > > >> class String > > >> alias :old_init :initialize > > >> def initialize() end > > >> end > > > > Oh, I see. I had to remove the undef_method(:initialize) call with > > > this approach. Alright, this feels a bit clunky, but it will do the > > > job. The only thing I'm going to change is to making old_init > > > private. :) > > > Can't you safely remove it once it has been renamed? > > Looks that way. This ran -w clean for me: > > class String > alias old_init initialize > def initialize > puts "hello" > end > remove_method(:old_init) > end Quick followup. For singleton methods you MUST use the "class << self" style for both the alias and the definition to avoid warnings: # This is -w clean class File class << self alias :basename_orig :basename def basename puts "hello" end remove_method(:basename_orig) end end # This is not class File class << self alias :basename_orig :basename end def self.basename puts "hello" end class << self remove_method(:basename_orig) end end Regards, Dan