gpt4 book ai didi

ruby - 从 ruby 新手返回命令

转载 作者:太空宇宙 更新时间:2023-11-03 17:56:56 25 4
gpt4 key购买 nike

我是第一次尝试返回函数。以下代码行显示没有输出。我试图弄清楚我的代码有什么问题。非常感谢您的意见。

def favourite_drink name
if name == "tea"
return "I love tea too!"
end

if name == "lemonade"
return "Stuff's refreshing, isn't it?"
end

if name == "coffee"
return "Dude, don't have too much of that stuff!"
end

"So what exactly is it that you like? (scratches head)"
end

favourite_drink "tea"

最佳答案

没有输出,因为您没有输出函数的结果。

puts favourite_drink("tea")

输出:

"I love tea too!"

您可能已经在 irb 中尝试过 Ruby,它是一个 REPL——一个读取-求值-打印循环。在 irb 中,如果您输入代码,您会看到:

 => "I love tea too!"

因为 irb 会自动显示您键入的任何值。在实际运行你的程序时,你需要特别要求输出你想要打印的任何内容。

关于ruby - 从 ruby 新手返回命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11753950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com