2017-02-07 38 views
-1
def wri(var) 
puts var 
end 
wri(hey) 

输出:main.rb:4:in“:未定义局部变量或方法hey' for main:Object (NameError)红宝石功能未定义局部变量或方法

哪里是错误?

+1

嗯..错误信息几乎告诉你一切。 'hey'是一个变量,而不是你想要的字符串。使用'wri(“嘿”)''。 –

回答

3

您将变量hey作为参数传递给方法wri()。你可能想要字符串'hey'

>def wri(var) 
> puts var 
>end 
>nil 
>wri('hey') 
hey 
=> nil 
>the_variable_hey = 'hey' 
=> 'hey' 
>wri(the_variable_hey) 
hey 
=> nil 
+0

只是一个友好的提示,没有必要在你的答案中包含irb-prompts,它实际上使代码更难阅读。所以在将来最好避免。无论如何,这个答案是正确的,所以我+1了。 –

相关问题