【发布时间】:2025-12-25 19:15:06
【问题描述】:
在 Ruby 中,我想创建一个接受用户输入的程序,并在程序创建的 .doc 文件中显示一条消息。我对 ruby 完全陌生,如果我完全不合时宜,请随意建议一个网站,我可以看看如何做到这一点。我的代码返回“3:in ' undefined method 'doc' for 0: Fixnum ”。该程序将按原样执行第一行代码并显示“你好,朋友。你叫什么名字?”在显示错误之前。
puts "Hello Friend. What is your name?"
testingruby = 0
File.open(testingruby.doc, 'w') {|f| f.write(doc) }
STDOUT.flush
name = gets.chomp
if name != "David"
f.puts "That's incorrect." + " " + name
else
f.puts "Hello " + name + "!" + " I hope you are well. Welcome to Ruby."
end
puts "Program will end in 5 seconds."
sleep 5 # seconds
$end
【问题讨论】:
标签: ruby user-input