Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

alias

module

ruby

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?

like image 307
Sean Mackesey Avatar asked Jun 18 '12 22:06

Sean Mackesey


People also ask

How do I use alias in Ruby?

To alias a method or variable name in Ruby is to create a second name for the method or variable. Aliasing can be used either to provide more expressive options to the programmer using the class or to help override methods and change the behavior of the class or object.

How do you reference a module in Ruby?

As with class methods, you call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons.

Can you instantiate a module in Ruby?

You can define and access instance variables within a module's instance methods, but you can't actually instantiate a module.

How do you create a module in Ruby?

Creating Modules in Ruby To define a module, use the module keyword, give it a name and then finish with an end . The module name follows the same rules as class names. The name is a constant and should start with a capital letter. If the module is two words it should be camel case (e.g MyModule).


2 Answers

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" 
like image 80
Michael Kohl Avatar answered Sep 21 '22 15:09

Michael Kohl


Michael's answer seems to solve your question... still, I read the question a bit differently and discovered something really nice that I thought worth sharing.

I understood your question as: "What do I do if I want to require two modules of the same name?", that is, how could I alias them if requiring both would result in a namespace clash? Because, as far as my understanding of Python's 'import ... as ...' goes, it also solves those kinds of problems. An example in Ruby:

#file a.rb module A   def self.greet     puts 'A'   end end  #file b.rb module A   def self.greet     puts 'other A'   end end 

Now if I would do this in a third file:

require_relative 'a' require_relative 'b'  A.greet # => other A 

the first A would be completely overridden by the A in b.rb. Using Michael's trick also won't help:

require_relative 'a' TMP_A = A A.greet # => A TMP_A.greet # => A require_relative 'b' TMP_A2 = A A.greet # => other A TMP_A2.greet # => other A TMP_A.greet # => other A :( 

Too bad. Then I thought, well, in Ruby there's the ubiquitous dup for making a clone of basically everything and without too much hope I just typed this and reran the program:

require_relative 'a' TMP_A = A.dup A.greet # => A TMP_A.greet # => A require_relative 'b' TMP_A2 = A A.greet # => other A TMP_A2.greet # => other A TMP_A.greet # => A :P 

That totally made my day, hope you guys appreciate it as much as well. Now that I think about it, it makes sense - a module is an object like any other after all, so why shouldn't dup work?

like image 23
emboss Avatar answered Sep 19 '22 15:09

emboss