Redefining initialize while staying -w clean

D

Daniel Berger

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
 
J

James Edward Gray II

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
 
A

Andrew Johnson

Daniel said:
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 :eek:ld_init :initialize
def initialize() end

regards,
andrew
 
D

Daniel Berger

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
 
D

Daniel Berger

Try aliasing the method before redefining it:

alias :eek:ld_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
 
J

James Edward Gray II

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

Remove the colons or swap alias with alias_method and add a comma.

James Edward Gray II
 
A

Andrew Johnson

Daniel said:
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 :eek:ld_init :initialize
def initialize() end
end

$ ruby -w init.rb
$

andrew
 
D

Daniel Berger

Perhaps I've misunderstood your case -- the following runs -w clean on
Ruby 1.8.6

$ cat init.rb

class String
alias :eek:ld_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
 
J

James Edward Gray II

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?

James Edward Gray II
 
D

Daniel Berger

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
 
D

Daniel Berger

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,239
Messages
2,571,200
Members
47,840
Latest member
Tiffany471

Latest Threads

Top