Funky alias problem

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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Wed, 28 Mar 2006, polypus wrote:

what is the best way?

--
share your knowledge. it's a way to achieve immortality.
- h.h. the 14th dali lama

thanks that's pretty

···

--
Posted via http://www.ruby-forum.com/.