# How to aliase a module method?

**URL:** <https://www.ruby-forum.com/t/how-to-aliase-a-module-method/88123>\
**Category:** Ruby\
**Created:** [March 8, 2007, 11:35am UTC](https://www.ruby-forum.com/t/how-to-aliase-a-module-method/88123 "2007-03-08T11:35:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [March 8, 2007, 11:35am UTC](https://www.ruby-forum.com/t/how-to-aliase-a-module-method/88123/1 "2007-03-08T11:35:29Z")

</div>

I am trying to aliase a module method to some local alias (to ease  
some template). I’ve tried a few variations of

alias escape\_latex MyUtils.escape\_latex

without luck so far. I could mixin the module or write a wrapper, but  
I’d like to know how to accomplish that with alias, or that it is not  
possible if that’s the case.

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [March 9, 2007, 12:10am UTC](https://www.ruby-forum.com/t/how-to-aliase-a-module-method/88123/2 "2007-03-09T00:10:30Z")

</div>

On Mar 8, 5:34 am, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to aliase a module method to some local alias (to ease  
> some template). I’ve tried a few variations of
> 
> alias escape\_latex MyUtils.escape\_latex
> 
> without luck so far. I could mixin the module or write a wrapper, but  
> I’d like to know how to accomplish that with alias, or that it is not  
> possible if that’s the case.

It’s possible, sort of. Try

module TheModule  
instance\_eval “alias escape\_latex MyUtils.escape\_latex”  
end

I use a similar technique (originally suggested by Minero A.,  
according to my notes) to replace Thread.critical and  
Thread.critical=. I’ve only tested it in 1.8.2 but I don’t think  
anything’s broken it since then.

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [March 9, 2007, 12:16am UTC](https://www.ruby-forum.com/t/how-to-aliase-a-module-method/88123/3 "2007-03-09T00:16:30Z")

</div>

On Fri, 9 Mar 2007 removed\_email\_address@domain.invalid wrote:

> It’s possible, sort of. Try
> 
> > – fxn  
> > module M  
> > class \<\< self  
> > alias\_method ‘dst’, ‘src’  
> > end  
> > end

-a

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [March 9, 2007, 12:18am UTC](https://www.ruby-forum.com/t/how-to-aliase-a-module-method/88123/4 "2007-03-09T00:18:31Z")

</div>

On 3/8/07, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to aliase a module method to some local alias (to ease  
> some template). I’ve tried a few variations of
> 
> alias escape\_latex MyUtils.escape\_latex
> 
> without luck so far. I could mixin the module or write a wrapper, but  
> I’d like to know how to accomplish that with alias, or that it is not  
> possible if that’s the case.

first of all, you need to use symbols for methods (actually, you just  
refer to the names of the methods):

alias :escape\_latex :MyUtils.escape\_latex

Can you see the problem: how you’d write the symbol for  
MyUtils.escape\_latex? This is not the way.  
Now, this is excerpt from Ryan D.’ Ruby QuickRef  
([Ruby | zenspider.com | by ryan davis](http://www.zenspider.com/Languages/Ruby/QuickRef.html)):

Aliasing

alias :new :old  
alias\_method :new, :old

Creates a new reference to whatever old referred to. old can be any  
existing method, operator, global. It may not be a local, instance,  
constant, or class variable.

The simple solution would be in this case (provided you don’t mind  
including the other methods of the module):

include MyUtils

PS: I’ve tried

module TheModule  
instance\_eval “alias escape\_latex MyUtils.escape\_latex”  
end

and I got:

(eval):1: parse error, unexpected ‘.’, expecting $  
alias escape\_latex MyUtils.escape\_latex  
^(pointing at the dot)

I don’t think it’d work, but I may be wrong, and will be glad if  
somebody  
will show the working code for this.

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [March 9, 2007, 3:04am UTC](https://www.ruby-forum.com/t/how-to-aliase-a-module-method/88123/5 "2007-03-09T03:04:48Z")

</div>

On Mar 9, 2007, at 12:17 AM, Jan S. wrote:

> first of all, you need to use symbols for methods (actually, you just  
> refer to the names of the methods):

No, that’s not true.

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [March 9, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/how-to-aliase-a-module-method/88123/6 "2007-03-09T17:20:52Z")

</div>

On 3/8/07, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > possible if that’s the case.  
> > > anything’s broken it since then.  
> > > alias foo Math.sqrt  
> > > Because alias requires that both arguments be names (or symbols), Note  
> > > that it’s not a method but is directly performed by the ruby  
> > > interpreter.

irb(main):001:0\> alias foo Math.sqrt  
SyntaxError: compile error  
(irb):1: parse error, unexpected ‘.’, expecting $  
alias foo Math.sqrt  
^  
from (irb):1

You’re not even getting to eval.c, it’s failing to parse.

Module#alias\_method requires that it’s aruments be symbols  
cooresponding to the name.

The other problem is that you are trying to make a name in a different  
scope than the name you are aliasing. As far as I can see this isn’t  
possible.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [March 9, 2007, 3:33am UTC](https://www.ruby-forum.com/t/how-to-aliase-a-module-method/88123/7 "2007-03-09T03:33:51Z")

</div>

On Mar 9, 2007, at 12:10 AM, removed\_email\_address@domain.invalid wrote:

> It’s possible, sort of. Try
> 
> module TheModule  
> instance\_eval “alias escape\_latex MyUtils.escape\_latex”  
> end
> 
> I use a similar technique (originally suggested by Minero A.,  
> according to my notes) to replace Thread.critical and  
> Thread.critical=. I’ve only tested it in 1.8.2 but I don’t think  
> anything’s broken it since then.

I don’t understand that technique (it is not working here). In fact  
what I don’t understand is alias.

In irb we can do

alias foo sprintf

Why can’t we say

alias foo Math.sqrt

? Having a quick glance at eval.c I think the problem is that alias  
is syntax (that’s why it does not need a comma between its  
“arguments”) and I guess here’s the key:

case NODE\_ALIAS:  
if (NIL\_P(ruby\_class)) {  
rb\_raise(rb\_eTypeError, “no class to make alias”);  
}  
rb\_alias(ruby\_class, rb\_to\_id(rb\_eval(self, node-\>u1.node)),  
rb\_to\_id(rb\_eval(self, node-\>u2.node)));

– fxn
