Alias_method problem

Hi,
The below class works ok for single “require”

class A

method “newone” is defined in a different file.

alias_method :oldone, :newone

def newone
oldone
end

end

But if for some reason the class is reloaded, The “oldone” will
point to the “newone” that is defined as above(calling the “oldone”)
instead of the originally defined one.
Calling “newone” after the reloading ends up with stack overflow.

I assume this had been brought up before. Any fix or workaround?

Thanks

-Ted

I too have run in to this and implemented an ugly kludge to deal with it
(instead of taking the time to figure it out).

class A
$aliased = $aliased || 0

if $aliased == 0 then
# method “newone” is defined in a different file.
alias_method :oldone, :newone
def newone
oldone
end
end
$aliased += 1
end

···

-----Original Message-----
From: mengx@nielsenmedia.com [mailto:mengx@nielsenmedia.com]
Sent: Tuesday, July 16, 2002 3:01 PM
To: ruby-talk ML
Subject: alias_method problem

Hi,
The below class works ok for single “require”

class A

method “newone” is defined in a different file.

alias_method :oldone, :newone

def newone
oldone
end

end

But if for some reason the class is reloaded, The “oldone” will
point to the “newone” that is defined as above(calling the “oldone”)
instead of the originally defined one.
Calling “newone” after the reloading ends up with stack overflow.

I assume this had been brought up before. Any fix or workaround?

Thanks

-Ted

mengx@nielsenmedia.com wrote:

But if for some reason the class is reloaded, The “oldone” will
point to the “newone” that is defined as above(calling the “oldone”)
instead of the originally defined one.
Calling “newone” after the reloading ends up with stack overflow.

I assume this had been brought up before. Any fix or workaround?

It was discussed starting at:
http://ruby-talk.org/34131

What about putting the alias operation in a different file from the rest
of the class def?

=== A-alias.rb ===

class A
alias …
end

=== A.rb ===

require “A-alias”

class A
def …
end
end

···

============

This way, you can always reload A.rb without re-aliasing.

Hmmmm… I haven’t tested this, but here’s a
thought:

Could you test the value of “defined? oldone”
rather than resorting to a global variable?
Still a kludge, but maybe less so…

Hal

···

----- Original Message -----
From: “Steve Tuckner” STUCKNER@MULTITECH.COM
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, July 16, 2002 3:09 PM
Subject: RE: alias_method problem

I too have run in to this and implemented an ugly kludge to deal with it
(instead of taking the time to figure it out).

class A
$aliased = $aliased || 0

if $aliased == 0 then
# method “newone” is defined in a different file.
alias_method :oldone, :newone
def newone
oldone
end
end
$aliased += 1
end

I have another trick.

require “noreload”

class Foo
include NoReload

  alias_method :oldone, :newone
  def newone
     oldone
  end

no_reload_upto_here!

end

noreload uses a continuation but not tested enough.

% cat noreload.rb

class ReloadError < StandardError
end

module NoReload
def self.append_features(mod)
super
mod.class_eval do
unless @no_reload_included_position
@no_reload_included_position = caller[4]
else
if @no_reload_included_position == caller[4]
@no_reload_upto_here or raise ReloadError,
“`no_reload_upto_here!’ wasn’t called in previous definition”,
caller[3…-1]
@no_reload_upto_here.call
end
end

    def mod.no_reload_upto_here!
      @no_reload_upto_here and raise ReloadError,
        "`no_reload_upto_here!' called twice", caller[0]
      callcc{|@no_reload_upto_here|}
    end
  end
end

end

Full version:
http://www.notwork.org/~gotoken/ruby/p/as-is/noreload.rb
http://www.notwork.org/~gotoken/ruby/p/as-is/noreload-test.rb

···

At Wed, 17 Jul 2002 05:09:08 +0900, Steve Tuckner wrote:

I too have run in to this and implemented an ugly kludge to deal with it
(instead of taking the time to figure it out).