Funky alias problem


#1

hi,

if i write

module M
def foo
old_foo
end
end

class X
def foo
puts X
end
def extend_yourself
alias old_foo foo
extend M
end
end

class Y < X
def foo
puts Y
end
end

x = X.new
y = Y.new
x.foo -> X
y.foo -> Y
x.extend_yourself
y.extend_yourself
x.foo -> X
y.foo -> X !!!

the behaviour i want though is

y.foo -> Y

what is the best way?

thanks many


#2

well solving my own problem, i think the simplest way of doing this is
probably

module M
def foo
puts M
@old_foo.call
end
end

class X
def foo
puts X
end
def ext
@old_foo = method :foo
end
end

class Y < X
def foo
puts Y
end
end

anyway, still keen to hear any second opinions.


#3

On Wed, 28 Mar 2006, polypus wrote:

what is the best way?

harp:~ > cat a.rb

module M
def foo
old_foo
end
def self.extend_object object
object.class.module_eval{
alias old_foo foo
}
super
end
end

class X
def foo
puts X
end
def extend_yourself
extend M
end
end

class Y < X
def foo
puts Y
end
end

x = X.new
y = Y.new
x.foo #-> X
y.foo #-> Y
x.extend_yourself
y.extend_yourself
x.foo #-> X
y.foo #-> Y

harp:~ > ruby a.rb
X
Y
X
Y

hth.

-a


#4

thanks that’s pretty