我应该在我的 ruby​​ 脚本中定义一个 main 方法吗? [英] Should I define a main method in my ruby scripts?

查看:36
本文介绍了我应该在我的 ruby​​ 脚本中定义一个 main 方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,一个模块没有必须有一个main函数,但通常的做法是使用以下习语:

In python, a module doesn't have to have a main function, but it is common practice to use the following idiom:

def my_main_function():
    ... # some code

if __name__=="__main__":  # program's entry point
    my_main_function()

我知道 Ruby 也没有有一个 main 方法,但是有什么我应该遵循的最佳实践吗?我应该将我的方法命名为 main 还是什么?

I know Ruby doesn't have to have a main method either, but is there some sort of best practice I should follow? Should I name my method main or something?

关于主要方法的维基百科页面并没有真正帮助我.

作为旁注,我还在 python 中看到了以下习语:

As a side-note, I have also seen the following idiom in python:

def my_main_function(args=[]):
    ... # some code

if __name__=="__main__":  # program's entry point
    import sys
    sys.exit(my_main_function(sys.argv))

推荐答案

我经常使用

if __FILE__ == $0
  x = SweetClass.new(ARGV)
  x.run # or go, or whatever
end

所以是的,你可以.这取决于你在做什么.

So yes, you can. It just depends on what you are doing.

这篇关于我应该在我的 ruby​​ 脚本中定义一个 main 方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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