如何在 MAC OS 终端中运行 Ruby 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12920733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run Ruby programs in MAC OS Terminal
提问by Ducati007
Possible Duplicate:
How to run ruby files?
可能重复:
如何运行 ruby 文件?
I am starting to learn Ruby and having a hard time running the Ruby classes in the Terminal.
我开始学习 Ruby 并且很难在终端中运行 Ruby 类。
I created a class in the Sublime Text editor, just "hello world". I can compile using ruby hello.rb,
but how do I execute it?
我在 Sublime Text 编辑器中创建了一个类,就是“hello world”。我可以使用 编译ruby hello.rb,但我如何执行它?
I went to the terminal in my root directory and typed rails cwhich gave me a console. Could some one please tell me how to create an instance? Which console do I use?
我转到根目录中的终端并输入rails c它给了我一个控制台。有人可以告诉我如何创建一个实例吗?我使用哪个控制台?
回答by pje
Ruby is interpreted, so you don't need to worry about a separate compile step. ruby hello.rbisthe execution command.
Ruby 是解释型的,因此您无需担心单独的编译步骤。ruby hello.rb是执行命令。
The standard interactive shell (REPL) is irb.
标准交互式 shell (REPL) 是irb.
回答by Demagog
I think, this is very simple task.
Paste in terminal ruby <your script name>.rb
我认为,这是非常简单的任务。粘贴到终端ruby <your script name>.rb
This is all. Ruby is interpreted lang. Compiler doesn`t exist at all. Only interpreter. I use Ruby only few times, but I think, you must run your method hello. Your code only create the class and nothing else. You should firstly learn Ruby and then RoR.
这就是全部。Ruby 被解释为 lang。编译器根本不存在。只有翻译。我只使用 Ruby 几次,但我认为,你必须运行你的方法 hello。您的代码只创建类,没有别的。你应该先学习Ruby,然后再学习RoR。
回答by echristopherson
As others have pointed out, running ruby hello.rbdoesrun the script; there is no compilation involved (except behind the scenes in the Ruby virtual machine, but you don't need to concern yourself with that).
正如其他人指出的那样,运行ruby hello.rb确实会运行脚本;不涉及编译(除了在 Ruby 虚拟机的幕后,但您不必担心)。
Based on the code of the file which you gave in a comment (I've put in line breaks and indentation):
根据您在评论中给出的文件代码(我已放入换行符和缩进):
class Hello
def say
puts "hello World"
end
end
... the reason your script doesn't seem to do anything is that it only defines a class and a method but doesn't instantiate the class or call the method. You had the right idea (in another comment) to call h = Hello.new(); after that you can put h.sayand it will say "hello World".
...您的脚本似乎没有做任何事情的原因是它只定义了一个类和一个方法,但没有实例化该类或调用该方法。你有正确的想法(在另一条评论中)打电话h = Hello.new();之后你可以放h.say,它会说“你好世界”。
(Parentheses are usually not required, including in these two method calls; but sometimes they are important. There are varying conventions, but most Rubyists skip them when calling methods without any arguments, like newand sayhere.)
(括号通常不是必需的,包括在这两个方法调用中;但有时它们很重要。有不同的约定,但大多数 Ruby 专家在调用没有任何参数的方法时会跳过它们,例如new和say这里。)
EDIT:
rails cis for Ruby on Rails, which is a separate entity from the Ruby language (although it's written in Ruby).
编辑:
rails c适用于 Ruby on Rails,它是一个独立于 Ruby 语言的实体(尽管它是用 Ruby 编写的)。

