是否可以在Ruby中为模块创建别名?

时间:2023-01-15 21:50:55

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

在Python中,您可以使用“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?

但我似乎无法找到红宝石的等价物。我知道你可以包含而不是需要一个模块,但这会冒命名空间冲突的风险。有没有相当于Python模块别名?

2 个解决方案

#1


41  

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

Ruby中的模块并不是那么特别,所以你可以将它们分配给另一个常量:

[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"

#2


23  

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 *? Because, as far as my understanding of Python's 'import ... as ...' goes, it also solves those kinds of problems. An example in Ruby:

我把你的问题理解为:“如果我想要两个同名模块,我该怎么办?”,也就是说,如果要求两者都会导致命名空间冲突,我怎么能将它们别名?因为,就我对Python的'import ...... as ...'的理解而言,它也解决了这些问题。 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:

第一个A将被b.rb中的A完全覆盖。使用迈克尔的技巧也无济于事:

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:

太糟糕了。然后我想,好吧,在Ruby中,无处不在的重复用于克隆基本上所有内容而没有太多希望我只需键入此内容并重新启动程序:

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?

这完全取决于我的一天,希望你们能够尽情地欣赏它。现在我想一想,它是有道理的 - 毕竟模块是一个像任何其他的对象,所以为什么不应该重复工作?

#1


41  

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

Ruby中的模块并不是那么特别,所以你可以将它们分配给另一个常量:

[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"

#2


23  

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 *? Because, as far as my understanding of Python's 'import ... as ...' goes, it also solves those kinds of problems. An example in Ruby:

我把你的问题理解为:“如果我想要两个同名模块,我该怎么办?”,也就是说,如果要求两者都会导致命名空间冲突,我怎么能将它们别名?因为,就我对Python的'import ...... as ...'的理解而言,它也解决了这些问题。 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:

第一个A将被b.rb中的A完全覆盖。使用迈克尔的技巧也无济于事:

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:

太糟糕了。然后我想,好吧,在Ruby中,无处不在的重复用于克隆基本上所有内容而没有太多希望我只需键入此内容并重新启动程序:

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?

这完全取决于我的一天,希望你们能够尽情地欣赏它。现在我想一想,它是有道理的 - 毕竟模块是一个像任何其他的对象,所以为什么不应该重复工作?