在python中,模块不必具有 main函数,但通常的做法是使用以下习语:
def my_main_function():
... # some code
if __name__=="__main__": # program's entry point
my_main_function()
Run Code Online (Sandbox Code Playgroud)
我知道红宝石不具备有一个main方法要么,但有某种我应遵循最佳实践?我应该说出我的方法main或其他什么?
作为旁注,我在python中也看到了以下习语:
def my_main_function(args=[]):
... # some code
if __name__=="__main__": # program's entry point
import sys
sys.exit(my_main_function(sys.argv))
Run Code Online (Sandbox Code Playgroud)
All*_*lyn 76
我经常使用
if __FILE__ == $0
x = SweetClass.new(ARGV)
x.run # or go, or whatever
end
Run Code Online (Sandbox Code Playgroud)
是的,你可以.这取决于你在做什么.
小智 49
我总是发现$PROGRAM_NAME比使用更具可读性$0.有一半时间,我看到像这样的"Perl-like"全局变量,我必须去查看它们.
if __FILE__ == $PROGRAM_NAME
# Put "main" code here
end
Run Code Online (Sandbox Code Playgroud)
您应该将库代码放在bin /中需要库代码的lib /和可执行文件中.这具有与RubyGems的封装方法兼容的额外优势.
一个常见的模式是lib/application.rb(或者最好是一个更适合您域名的名称)和bin/application,它包含:
require 'application'
Application.run(ARGV)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22920 次 |
| 最近记录: |