11
如果在ruby文件我定義一個函數,像這樣:紅寶石等同於Python的__main__
def tell_the_truth()
puts "truth"
end
是有一個相當於Python的主?
if __name__ == "__main__":
tell_the_truth()
它只是簡單地調用文件內的函數嗎?
tell_the_truth
如果在ruby文件我定義一個函數,像這樣:紅寶石等同於Python的__main__
def tell_the_truth()
puts "truth"
end
是有一個相當於Python的主?
if __name__ == "__main__":
tell_the_truth()
它只是簡單地調用文件內的函數嗎?
tell_the_truth
我相信這將工作:
if __FILE__ == $0
tell_the_truth()
end
if __FILE__ == $PROGRAM_NAME
tell_the_truth()
end
原因Python中的'if'是防止函數被調用,如果該文件包含到另外一個,你的Ruby示例不行(據我所知) – 2010-07-19 21:44:03
謝謝邁克爾。我明白爲什麼在python背後。爲了進行比較,我嘗試了將tell_the_truth()放在函數定義的下方,然後使用「require」從另一個ruby文件導入該文件。這打印出「真相」。因此,頂級語句似乎在導入文件時運行。 – Rodrigue 2010-07-20 08:10:48