# Funky alias problem

**URL:** <https://www.ruby-forum.com/t/funky-alias-problem/89680>\
**Category:** Ruby\
**Created:** [March 29, 2006, 1:56am UTC](https://www.ruby-forum.com/t/funky-alias-problem/89680 "2006-03-29T01:56:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![polypus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@polypus](https://www.ruby-forum.com/u/polypus)\
**Post date:** [March 29, 2006, 1:56am UTC](https://www.ruby-forum.com/t/funky-alias-problem/89680/1 "2006-03-29T01:56:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![polypus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@polypus](https://www.ruby-forum.com/u/polypus)\
**Post date:** [March 29, 2006, 2:43am UTC](https://www.ruby-forum.com/t/funky-alias-problem/89680/2 "2006-03-29T02:43:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![polypus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@polypus](https://www.ruby-forum.com/u/polypus)\
**Post date:** [March 29, 2006, 4:35am UTC](https://www.ruby-forum.com/t/funky-alias-problem/89680/3 "2006-03-29T04:35:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![polypus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@polypus](https://www.ruby-forum.com/u/polypus)\
**Post date:** [March 29, 2006, 6:08am UTC](https://www.ruby-forum.com/t/funky-alias-problem/89680/4 "2006-03-29T06:08:22Z")

</div>

thanks that’s pretty
