【问题标题】:Ruby - How to create a program that will create a .doc file and display user input?Ruby - 如何创建将创建 .doc 文件并显示用户输入的程序?
【发布时间】: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


    【解决方案1】:

    尝试将testingruby.doc 包装在一个字符串中:"testingruby.doc"

    您已经定义了一个 Fixnum testingruby,而程序认为您正在尝试访问 testingrubydoc 属性,而您只是想定义一个文件名。

    【讨论】:

    • 太棒了。我鼓励您点击投票计数器下方的小复选框以“接受”这个答案,并让以后看到这个答案的人受益。