2013-08-07 44 views
0
# code.rb 
def hello 
    puts "hello" 
end 

:$ ruby code.rb 

控制台上没有输出!我使用Ubuntu 13.04。为什么在控制台上禁止Ruby输出?

如果我在IRB中运行相同的代码,它的工作原理!

+1

什么在这种情况下,IRB输出? –

回答

5

你要打电话给你的代码,你只是定义一个方法:

# code.rb 
    def hello 
     puts "hello" 
    end 

    hello 

$ ruby code.rb 
1

您需要调用的方法,在这种情况下,hello脚本:

def hello 
    puts "hello" 
end 

hello 
1

你定义一个方法,但你永远不会调用它。试试这个:

# code.rb 
def hello 
    puts "hello" 
end 

hello 

运行:

:$ ruby code.rb