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 c
which gave me a console. Could some one please tell me how to create an instance? Which console do I use?
我去了根目录下的终端,输入了rails c,它给了我一个控制台。有人可以告诉我如何创建一个实例?我使用哪个控制台?
3 个解决方案
#1
10
Ruby is interpreted, so you don't need to worry about a separate compile step. ruby hello.rb
is the execution command.
解释Ruby,因此您不必担心单独的编译步骤。 ruby hello.rb是执行命令。
The standard interactive shell (REPL) is irb
.
标准交互式shell(REPL)是irb。
#2
1
I think, this is very simple task. Paste in terminal ruby <your script name>.rb
我想,这是一项非常简单的任务。粘贴到终端ruby <您的脚本名称> .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几次,但我想,你必须运行你的方法你好。您的代码只创建类而不创建其他内容。你应该首先学习Ruby然后学习RoR。
#3
0
As others have pointed out, running ruby hello.rb
does run 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.say
and 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 new
and say
here.)
(括号通常不是必需的,包括在这两个方法调用中;但有时它们很重要。有不同的约定,但大多数Rubyist在调用没有任何参数的方法时跳过它们,比如new,并在这里说。)
EDIT: rails c
is 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编写的)。
#1
10
Ruby is interpreted, so you don't need to worry about a separate compile step. ruby hello.rb
is the execution command.
解释Ruby,因此您不必担心单独的编译步骤。 ruby hello.rb是执行命令。
The standard interactive shell (REPL) is irb
.
标准交互式shell(REPL)是irb。
#2
1
I think, this is very simple task. Paste in terminal ruby <your script name>.rb
我想,这是一项非常简单的任务。粘贴到终端ruby <您的脚本名称> .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几次,但我想,你必须运行你的方法你好。您的代码只创建类而不创建其他内容。你应该首先学习Ruby然后学习RoR。
#3
0
As others have pointed out, running ruby hello.rb
does run 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.say
and 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 new
and say
here.)
(括号通常不是必需的,包括在这两个方法调用中;但有时它们很重要。有不同的约定,但大多数Rubyist在调用没有任何参数的方法时跳过它们,比如new,并在这里说。)
EDIT: rails c
is 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编写的)。