Is it possible to make an alias for a module in Ruby?

Sean Mackesey picture Sean Mackesey · Jun 19, 2012 · Viewed 9.9k times · Source

In Python, you can set an alias for a module with 'as':

import mymodule as mm

But I can't seem to find an equivalent for ruby. I know that you can include rather than require a module, but this risks namespace collisions. Is there any equivalent to Python module aliases?

Answer

Michael Kohl picture Michael Kohl · Jun 19, 2012

Modules in Ruby aren't really that special, so you can just assign them to another constant:

[4] (pry) main: 0> module TestModule
[4] (pry) main: 0*   def self.foo
[4] (pry) main: 0*     "test"
[4] (pry) main: 0*   end  
[4] (pry) main: 0* end  
=> nil
[5] (pry) main: 0> tm = TestModule
=> TestModule
[6] (pry) main: 0> tm.foo
=> "test"