Finding out if a method is already redefined

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

···

--
Michael Schuerig There is no matrix,
mailto:michael@schuerig.de only reality.
http://www.schuerig.de/michael/ --Lawrence Fishburn

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

···

On Sun, 18 Sep 2005, Michael Schuerig wrote:

--
David A. Black
dblack@wobblini.net

David A. Black wrote:

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:

[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

···

On Sun, 18 Sep 2005, Michael Schuerig wrote:

--
Michael Schuerig There is no matrix,
mailto:michael@schuerig.de only reality.
Michael Schürig | Sentenced to making sense --Lawrence Fishburn