在Ruby中,我想创建一个接受用户输入的程序,并在程序创建的.doc文件中显示一条消息。我对ruby来说是全新的,如果我完全不合时宜建议一个网站,我可以看看如何做到这一点。我的代码返回“3:in'undefined method'doc'for 0:Fixnum”。该程序将按原样执行第一行代码并显示“Hello Friend。你叫什么名字?”在显示错误之前。
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
答案 0 :(得分:1)
尝试将testingruby.doc
包裹在字符串中:"testingruby.doc"
。
您已经定义了Fixnum testingruby
,当您尝试定义文件名时,程序会认为您正在尝试访问doc
的{{1}}属性。