Finding out if a method is already redefined

M

Michael Schuerig

My problem occurs in the context of Rails, but is really core Ruby. In
its "development environment" Rails uses load, not require, to load
files anew for each request. This is to enable changes to the running
application. I'm running into a problem with this mechanism when
redefining a method *and* referring to the old method in the redefined
one. When this redefinition happens twice, it results in an infinite
recursion. I've tried to guard the redefinition with a check for the
new method, but this doesn't work, the new alias is not recognized.

unless method_defined?:)method_new)
alias_method :method_old, :method
def method_new(options = {})
...
method_old(options)
end
alias_method :method, :method_new
end

What do I have to do to make this work?

Michael
 
D

David A. Black

Hi --

My problem occurs in the context of Rails, but is really core Ruby. In
its "development environment" Rails uses load, not require, to load
files anew for each request. This is to enable changes to the running
application. I'm running into a problem with this mechanism when
redefining a method *and* referring to the old method in the redefined
one. When this redefinition happens twice, it results in an infinite
recursion. I've tried to guard the redefinition with a check for the
new method, but this doesn't work, the new alias is not recognized.

unless method_defined?:)method_new)
alias_method :method_old, :method
def method_new(options = {})
...
method_old(options)
end
alias_method :method, :method_new
end

What do I have to do to make this work?

I can't duplicate your problem. Here's what I'm doing:

# l.rb:
class C
def meth
puts "old"
end
end
c = C.new
c.meth
load 'load.rb'
c.meth
load 'load.rb'
c.meth

# load.rb
class C
unless method_defined?:)meth_new)
alias_method :meth_old, :meth
def meth_new
puts "new"
meth_old
end
alias_method :meth, :meth_new
end
end

# output of running l.rb:
old
new
old
new
old


David
 
M

Michael Schuerig

David said:
Hi --



I can't duplicate your problem. Here's what I'm doing:
[example snipped]

Yes, I know, I tried the same and outside of Rails it works nicely.
Apparently, Rails does more things than simply re-loading.

Michael
 

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
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top