# Alias to module methods or class methods

**URL:** <https://rubytalk.org/t/alias-to-module-methods-or-class-methods/17404>\
**Category:** ruby-talk\
**Created:** [4 April 2005 11:48 UTC](https://rubytalk.org/t/alias-to-module-methods-or-class-methods/17404 "2005-04-04T11:48:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geert\_Fannes1](https://avatars.discourse-cdn.com/v4/letter/g/ce73a5/32.png) [@Geert\_Fannes1](https://rubytalk.org/u/Geert_Fannes1)\
**Post date:** [4 April 2005 11:48 UTC](https://rubytalk.org/t/alias-to-module-methods-or-class-methods/17404/1 "2005-04-04T11:48:09Z")

</div>

Hello,

I tried to fix the Math.sqrt() domain error on windows I reported an  
hour ago by redefining the Math.sqrt method:

module Math

&nbsp;&nbsp;alias sqrt\_old sqrt

&nbsp;&nbsp;def Math.sqrt(x)

&nbsp;&nbsp;&nbsp;&nbsp;begin

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Math.sqrt\_old(x)

&nbsp;&nbsp;&nbsp;&nbsp;rescue Errno::EDOM

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0.0/0.0

&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;end

end

But this does not work. Apparently, I cannot create an alias to a module  
method. I tested a bit and found out I could not create aliases to class  
functions either. How can I create aliases to module methods and class  
methods?

Greetings,

Geert.

PS:I found some confusing thread about creating class method aliases so  
I ask it here again

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [4 April 2005 12:29 UTC](https://rubytalk.org/t/alias-to-module-methods-or-class-methods/17404/2 "2005-04-04T12:29:41Z")

</div>

Hi --

On Mon, 4 Apr 2005, Geert Fannes wrote:

> Hello,
> 
> I tried to fix the Math.sqrt() domain error on windows I reported an  
> hour ago by redefining the Math.sqrt method:
> 
> module Math
> 
> alias sqrt\_old sqrt
> 
> def Math.sqrt(x)
> 
> &nbsp;&nbsp;&nbsp;begin
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Math.sqrt\_old(x)
> 
> &nbsp;&nbsp;&nbsp;rescue Errno::EDOM
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0.0/0.0
> 
> &nbsp;&nbsp;&nbsp;end
> 
> end
> 
> end
> 
> But this does not work. Apparently, I cannot create an alias to a module  
> method. I tested a bit and found out I could not create aliases to class  
> functions either. How can I create aliases to module methods and class  
> methods?

You have to make the change in the class where the method is defined.  
Class and module methods are basically singleton methods -- that is,  
methods defined inside the singleton class of a particular object. In  
this case, the object is the Module object "Math" -- so you need to  
open up the singleton class of Math:

&nbsp;&nbsp;&nbsp;class \<\< Math  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias sqrt\_old sqrt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def sqrt(x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Math.sqrt\_old(x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue Errno::EDOM  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0.0/0.0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

(Note that a method definition gives you an implicit begin/end block,  
so you don't need to write one inside the method.)

David

> **···**
>
> --  
> David A. Black  
> dblack@wobblini.net
