funky alias problem

P

polypus

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
 
P

polypus

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.
 
A

ara.t.howard

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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top