是否可以在Ruby中为模块创建别名? [英] Is it possible to make an alias for a module in Ruby?

查看:71
本文介绍了是否可以在Ruby中为模块创建别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,您可以使用'as'为模块设置别名:

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

import mymodule as mm

但是我似乎找不到与红宝石相当的东西.我知道您可以include而不是require一个模块,但这有命名空间冲突的风险.有与Python模块别名等效的东西吗?

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?

推荐答案

Ruby中的模块并不是真的那么特殊,因此您可以将它们分配给另一个常量:

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"

这篇关于是否可以在Ruby中为模块创建别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆