相当于Ruby中的`if __name__ =='__main __'` [英] `if __name__ == '__main__'` equivalent in Ruby
问题描述
我是Ruby的新手.我正在从包含要继续单独使用的工具的模块中导入功能.在Python中,我只需要这样做:
I am new to Ruby. I'm looking to import functions from a module that contains a tool I want to continue using separately. In Python I would simply do this:
def a():
...
def b():
...
if __name__ == '__main__':
a()
b()
这使我可以运行程序或将其导入为模块,以分别使用a()
和/或b()
. Ruby中的等效范例是什么?
This allows me to run the program or import it as a module to use a()
and/or b()
separately. What's the equivalent paradigm in Ruby?
推荐答案
在Ruby中,我从野外看到(当然,不是一吨),这不是标准的Ruby设计模式.模块和脚本应该保持独立,因此,如果没有真正好的,干净的方法,我也不会感到惊讶.
From the Ruby I've seen out in the wild (granted, not a ton), this is not a standard Ruby design pattern. Modules and scripts are supposed to stay separate, so I wouldn't be surprised if there isn't really a good, clean way of doing this.
编辑:但这绝对不常见.
这篇关于相当于Ruby中的`if __name__ =='__main __'`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!