# Alias\_method 'stickiness'

**URL:** <https://www.ruby-forum.com/t/alias-method-stickiness/80175>\
**Category:** Ruby\
**Created:** [March 13, 2006, 1:48am UTC](https://www.ruby-forum.com/t/alias-method-stickiness/80175 "2006-03-13T01:48:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 13, 2006, 1:48am UTC](https://www.ruby-forum.com/t/alias-method-stickiness/80175/1 "2006-03-13T01:48:59Z")

</div>

any idea if this behaviour is intended or not?

mussel:~/eg/ruby/nrtlib/nrtlib-0.0.0 \> cat a.rb

# 

# the problem

# 

```
 module NRT
   class Subscription
     def process_incoming
       raise NotImplementedError
     end
     alias_method "run", "process_incoming"
   end
   class OLSSubscription < Subscription
     def process_incoming
       p 42
     end
   end
 end
 begin; NRT::OLSSubscription::new.run ;rescue Exception => e; p e; 

```

end

# 

# an easy solution

# 

```
 module NRT
   class Subscription
     def self::anonym dst, src
       module_eval %Q[def #{ dst }(*a, &b) #{ src }(*a, &b) end]
     end
     anonym "run", "process_incoming"
   end
 end
 NRT::OLSSubscription::new.run

```

mussel:~/eg/ruby/nrtlib/nrtlib-0.0.0 \> ruby a.rb  
#\<NotImplementedError: NotImplementedError\>  
42

this seems odd to me.

-a

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 13, 2006, 3:31am UTC](https://www.ruby-forum.com/t/alias-method-stickiness/80175/2 "2006-03-13T03:31:34Z")

</div>

Ara.T.Howard wrote:

> ```
> def process_incoming
> raise NotImplementedError
> end
> alias_method "run", "process_incoming"
> 
> ```

Unfortunately (?) what this^^^ does is define run to be a copy of the  
method process\_incoming, rather than define run as a method that  
delegates to process\_incoming.

I suppose this behavior is necessary if you want to use alias\_method to  
wrap an old method, regardless of what gets redefined in child classes.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 13, 2006, 3:55am UTC](https://www.ruby-forum.com/t/alias-method-stickiness/80175/3 "2006-03-13T03:55:40Z")

</div>

On Mon, 13 Mar 2006, Joel VanderWerf wrote:

> > ```
> > class Subscription
> > 
> > ```
> 
> wrap an old method, regardless of what gets redefined in child classes.  
> doh! yup - yer right. i like my ‘anonym’ method more and more.  
> perhaps an  
> option to alias\_method like

alias\_method “foo”, “bar”, :dup =\> false

or, more concise

alias\_method “foo”, “bar”, false

though i detest dangling bool arguments (method(true, false, true, true,  
false) acckkkk!)…

either that or alias\_method should be renamed copy\_method 😉

thoughts?

-a

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 13, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/alias-method-stickiness/80175/4 "2006-03-13T21:01:21Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > > #
> 
> alias\_method “foo”, “bar”, false
> 
> though i detest dangling bool arguments (method(true, false, true, true,  
> false) acckkkk!)…
> 
> either that or alias\_method should be renamed copy\_method 😉

I’ve just gotten used to using alias _only_ for metaprogramming, and not  
for ordinary API definition.

Maybe it would be nice if the alias keyword were reserved for the  
delegation style definition, like your anonym method, and the  
metaprogramming interface were something like this:

class Foo

# alias :m\_copy :m # old way

instance\_method\_at[:m\_copy] = instance\_method(:m) # new way

# now you can wrap :m\_copy however you want

end

This makes it more clear that you are making a copy, so it might be less  
confusing.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 13, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/alias-method-stickiness/80175/5 "2006-03-13T21:54:27Z")

</div>

On Tue, 14 Mar 2006, Joel VanderWerf wrote:

> # now you can wrap :m\_copy however you want
> 
> end
> 
> This makes it more clear that you are making a copy, so it might be less  
> confusing.

i like that. what do you think about

class Foo  
delegate :src, :dst  
end

??

-a
