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

2

32

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?

Inhibit answered 18/6, 2012 at 22:0 Comment(1)
require and include are very different in ruby. They are not interchangeable with each other.Lingerie
M
52

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"
Magee answered 18/6, 2012 at 22:4 Comment(1)
@Sean, you can assign them to any variable. You should use local variables because they won't pollute global scope. Also, you won't have to use the shift key all the time.Root
R
30

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?

Representational answered 18/6, 2012 at 22:36 Comment(4)
Nice, "a module is an object like any other after all" was kinda the point of my answer too. :-)Magee
@MichaelKohl Heh, true. I love it, there's so much to explore in Ruby :)Representational
@Representational Nice, this is a highly worthy addition to this thread. Will keep it in mind for when I need two modules with the same name.Inhibit
@Representational To help others find this answer, you might want to ask your interpretation of question and then post this as the answer to that.Poltergeist

© 2022 - 2024 — McMap. All rights reserved.