是否有“主要"?Ruby 中的方法就像在 C 中一样?
我是 Ruby 的新手,如果这听起来很傻,我深表歉意.
I'm new to Ruby, so apologies if this sounds really silly.
我似乎无法弄清楚如何编写主要"代码并在同一个文件中拥有方法(类似于 C).我最终得到一个主"文件,它加载一个包含所有方法的单独文件.我很感激这方面的任何指导.
I can't seem to figure out how to write a "main" code and have methods in the same file (similar to C). I end up with a "main" file which loads a seperate file that has all the methods. I appreciate any guidance on this.
我发现了以下 SO 帖子,但我不明白:我应该在我的 ruby 脚本中定义一个 main 方法吗?
I spotted the following SO post but I don't understand it: Should I define a main method in my ruby scripts?
虽然这没什么大不了的,但能够在同一个文件中查看所有相关代码更容易.谢谢.
While it's not a big deal, it's just easier being able to see all the relevant code in the same file. Thank you.
[-编辑-]
感谢所有回复的人 - 原来您只需要定义代码上方的所有方法.示例如下:
Thanks to everyone who responded - turns out you just need to define all the methods above the code. An example is below:
def callTest1
puts "in test 1"
end
def callTest2
puts "in test 2"
end
callTest1
callTest2
我认为这是有道理的,因为 Ruby 需要事先了解所有方法.这与 C 不同,C 有一个头文件清楚地列出了可用函数,因此可以在 main() 函数下定义它们
I think this makes sense as Ruby needs to know all methods beforehand. This is unlike C where there is a header file which clearly list the available functions and therefore, can define them beneath the main() function
再次感谢所有回复的人.
Again, thanks to everyone who responded.
@Hauleth 的回答是正确的:Ruby 中没有 main
方法或结构.我只是想在这里提供一个稍微不同的观点以及一些解释.
@Hauleth's answer is correct: there is no main
method or structure in Ruby. I just want to provide a slightly different view here along with some explanation.
当您执行ruby somefile.rb
时,Ruby 会执行somefile.rb
中的所有代码.因此,如果您有一个非常小的项目并希望它独立于单个文件,那么这样做绝对没有错:
When you execute ruby somefile.rb
, Ruby executes all of the code in somefile.rb
. So if you have a very small project and want it to be self-contained in a single file, there's absolutely nothing wrong with doing something like this:
# somefile.rb
class MyClass
def say_hello
puts "Hello World"
end
end
def another_hello
puts "Hello World (from a method)"
end
c = MyClass.new
c.say_hello
another_hello
并不是前两个块没有执行,只是在实际使用相应的类/方法之前看不到效果.
It's not that the first two blocks aren't executed, it's just that you don't see the effects until you actually use the corresponding class/method.
if __FILE__ == $0
位只是一种阻止代码的方法,如果该文件直接从命令行运行,您只想运行这些代码.__FILE__
是当前文件的名称,$0
是由 shell 执行的命令(尽管它足够聪明,可以删除 ruby
),因此比较两者可以准确地告诉您那:这是从命令行执行的文件吗?这有时由想要在文件中定义类/模块并提供使用它的命令行实用程序的编码人员完成.恕我直言,这不是很好的项目结构,但就像任何用例一样,这样做是完全合理的.
The if __FILE__ == $0
bit is just a way to block off code that you only want to run if this file is being run directly from the command line. __FILE__
is the name of the current file, $0
is the command that was executed by the shell (though it's smart enough to drop the ruby
), so comparing the two tells you precisely that: is this the file that was executed from the command line? This is sometimes done by coders who want to define a class/module in a file and also provide a command-line utility that uses it. IMHO that's not very good project structure, but just like anything there are use cases where doing it makes perfect sense.
如果您希望能够直接执行您的代码,您可以添加一个shebang 行
If you want to be able to execute your code directly, you can add a shebang line
#!/usr/bin/env ruby
# rest of somefile.rb
并使用 chmod +x somefile.rb
使其可执行(可选地重命名不带 .rb 扩展名).这并没有真正改变你的情况.if __FILE__ == $0
仍然有效并且可能仍然没有必要.
and make it executable with chmod +x somefile.rb
(optionally rename it without the .rb extension). This doesn't really change your situation. The if __FILE__ == $0
still works and still probably isn't necessary.
编辑
正如@steenslag 正确指出的那样,Ruby 中的顶级范围是一个名为 main
的 Object
.不过,它的行为有点古怪:
As @steenslag correctly points out, the top-level scope in Ruby is an Object
called main
. It has slightly funky behavior, though:
irb
>> self
=> main
>> self.class
=> Object
>> main
NameError: undefined local variable or method `main' for main:Object
from (irb):8
在您开始更深入地研究该语言之前,请不要担心这一点.如果您确实想了解更多有关此类内容的信息,元编程 Ruby 是很棒的阅读:)
Don't worry about this until you start to dig much deeper into the language. If you do want to learn lots more about this kind of stuff, Metaprogramming Ruby is a great read :)