Forum: Ruby Redefining initialize while staying -w clean

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-04-05 17:36
(Received via mailing list)
Hi all,

Is it possible to redefine initialize and stay -w clean? In win32-file-
stat, I've redefined initialize to suit my needs for MS Windows.

If I do "undef_method(:initialize)" and run with -w, I'll get:

"warning: undefining `initialize' may cause serious problem".

However, if don't do that, I get "warning: method redefined;
discarding old initialize".

Help! I'm trapped!

Thanks,

Dan
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-04-05 17:41
(Received via mailing list)
On Apr 5, 2007, at 10:35 AM, Daniel Berger wrote:

> If I do "undef_method(:initialize)" and run with -w, I'll get:
>
> "warning: undefining `initialize' may cause serious problem".

Does this work?

   remove_method(:initialize) if method_defined? :initialize

James Edward Gray II
5c841628b56df3a68984986e9f095d01?d=identicon&s=25 Andrew Johnson (andrew)
on 2007-04-05 17:47
Daniel Berger wrote:
> Hi all,
>
> Is it possible to redefine initialize and stay -w clean? In win32-file-
> stat, I've redefined initialize to suit my needs for MS Windows.
>
> If I do "undef_method(:initialize)" and run with -w, I'll get:
>
> "warning: undefining `initialize' may cause serious problem".
>
> However, if don't do that, I get "warning: method redefined;
> discarding old initialize".

Try aliasing the method before redefining it:

  alias :old_init :initialize
  def initialize() end

regards,
andrew
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-04-05 17:56
(Received via mailing list)
On Apr 5, 9:40 am, James Edward Gray II <j...@grayproductions.net>
wrote:
> On Apr 5, 2007, at 10:35 AM, Daniel Berger wrote:
>
> > If I do "undef_method(:initialize)" and run with -w, I'll get:
>
> > "warning: undefining `initialize' may cause serious problem".
>
> Does this work?
>
>    remove_method(:initialize) if method_defined? :initialize

Unfortunately, using remove_method just issues a different message:
"warning: removing `initialize' may cause serious problem"

Regards,

Dan
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-04-05 17:57
(Received via mailing list)
On Apr 5, 9:47 am, Andrew Johnson <ajohn...@cpan.org> wrote:
> > However, if don't do that, I get "warning: method redefined;
> > discarding old initialize".
>
> Try aliasing the method before redefining it:
>
>   alias :old_init :initialize
>   def initialize() end

That doesn't appear to work. Do you have a code snippet where it does
work? Perhaps I've missed something.

Thanks,

Dan
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-04-05 18:02
(Received via mailing list)
On Apr 5, 2007, at 10:55 AM, Daniel Berger wrote:

>>> "warning: undefining `initialize' may cause serious problem".
> work? Perhaps I've missed something.
Remove the colons or swap alias with alias_method and add a comma.

James Edward Gray II
5c841628b56df3a68984986e9f095d01?d=identicon&s=25 Andrew Johnson (andrew)
on 2007-04-05 18:06
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

  $ ruby -w init.rb
  $

andrew
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-04-05 18:21
(Received via mailing list)
On Apr 5, 10:06 am, Andrew Johnson <ajohn...@cpan.org> wrote:
>   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. :)

Many thanks,

Dan
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-04-05 18:32
(Received via mailing list)
On Apr 5, 2007, at 11:20 AM, Daniel Berger wrote:

>>
> private. :)
Can't you safely remove it once it has been renamed?

James Edward Gray II
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-04-05 18:52
(Received via mailing list)
On Apr 5, 10:30 am, James Edward Gray II <j...@grayproductions.net>
wrote:
> >>> does
> >>     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

Regards,

Dan
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-04-05 19:21
(Received via mailing list)
On Apr 5, 10:49 am, "Daniel Berger" <djber...@gmail.com> wrote:
> > >> Daniel Berger wrote:
>
> > 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
This topic is locked and can not be replied to.