简单Ruby程序不产生输出



我在Mac OS上用TextMate写了一个基本的ruby程序:

def hello
puts " This works!"
end

命名为Check-it.rb

我打开一个终端会话,cd到存储程序的目录。

然后输入

ruby Check-it.rb

什么也没有。

ruby -v

显示了版本,所以它在那里。但是对于这个和其他Ruby程序,什么都没有出现。

正如其他人已经指出的那样。文件

中的代码
def hello
puts " This works!"
end

定义一个叫做hello的方法,输出一个字符串。但是这个方法从来不会被称为。要调用并运行该方法,请将文件中的代码改为

def hello                          # this block defines the `hello` method
puts " This works!"
end
hello                              # this line calls the method `hello`

我想你根本没有调用这个方法。调用这个方法,然后运行你的代码,它将工作。

最新更新