# Inheritence of aliases methods - suprise!

**URL:** <https://www.ruby-forum.com/t/inheritence-of-aliases-methods-suprise/53467>\
**Category:** Ruby\
**Created:** [February 6, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/inheritence-of-aliases-methods-suprise/53467 "2006-02-06T22:07:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 6, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/inheritence-of-aliases-methods-suprise/53467/1 "2006-02-06T22:07:19Z")

</div>

this suprised me today:

```
 harp:~ > cat a.rb
 class A
   def assertion
     raise NotImplementedError
   end
   alias_method "assertion?", "assertion"
 end

 class B < A
   def assertion
     true
   end
 end

 B::new.assertion?

 harp:~ > ruby a.rb
 a.rb:3:in `assertion?': NotImplementedError (NotImplementedError)
         from a.rb:14

```

this only way i can seem to make this work is through some  
self::inherited  
hacks or to actually define assertion? in the base class. is there no  
clean  
way to inherit aliases?

regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 6, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/inheritence-of-aliases-methods-suprise/53467/2 "2006-02-06T22:25:49Z")

</div>

On Feb 6, 2006, at 1:06 PM, removed\_email\_address@domain.invalid wrote:

> this suprised me today:

$ parse\_tree\_show -  
class A  
def assertion  
raise NotImplementedError  
end  
alias\_method “assertion?”, “assertion”  
end

```
 class B < A
   def assertion
     true
   end
 end

```

[[:class, :B, :A, [:defn, :assertion, [:scope, [:block, [:args],  
[:true]]]]],  
[:class,  
:A,  
:Object,  
[:defn,  
:assertion,  
[:scope,  
[:block,  
[:args],  
[:fcall, :raise, [:array, [:const, :NotImplementedError]]]]]],  
[:defn,  
:“assertion?”,  
[:fbody,  
[:scope,  
[:block,  
[:args],  
[:fcall, :raise, [:array, [:const, :NotImplementedError]]]]]]]]]

> this only way i can seem to make this work is through some  
> self::inherited  
> hacks or to actually define assertion? in the base class. is there  
> no clean  
> way to inherit aliases?

alias copies the method, it doesn’t make a pointer to the method.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 6, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/inheritence-of-aliases-methods-suprise/53467/3 "2006-02-06T22:25:51Z")

</div>

On Feb 6, 2006, at 4:06 PM, removed\_email\_address@domain.invalid wrote:

> ```
> a.rb:3:in `assertion?': NotImplementedError (NotImplementedError)
> 
> ```
> 
> -a
> 
> –  
> happiness is not something ready-made. it comes from your own  
> actions.
> 
> - h.h. the 14th dali lama

This makes sense, If you consider the common idiom of alias’ing a  
method to wrap additional functionality around it. If alias(\_method)  
didn’t work like that you couldn’t use it like this. alias\_method  
doesn’t work like assignment in ruby, its more like it creates a new  
method with the same source as the original method.

As a poorly conceived alternative:

% cat a.rb  
class Class  
def shallow\_alias(new\_name, current\_name)  
self.module\_eval \<\<-END  
def #{new\_name}(\*args, &block)  
#{current\_name}(\*args, &block)  
end  
END  
end  
end

class A  
def assertion  
raise NotImplementedError  
end  
shallow\_alias “assertion?”, “assertion”  
end

class B \< A  
def assertion  
true  
end  
end

B.new.assertion?

% ruby a.rb  
%

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 6, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/inheritence-of-aliases-methods-suprise/53467/4 "2006-02-06T22:59:48Z")

</div>

On Tue, 7 Feb 2006, Logan C. wrote:

> def shallow\_alias(new\_name, current\_name)  
> raise NotImplementedError  
> B.new.assertion?
> 
> % ruby a.rb  
> %

yeah - this is pretty much what i did. for some reason i had always  
assume  
that alias\_method worked this way.

regards.

-a
