我刚开始使用Ruby,并在字符串周围添加双引号。
print "Enter the name of the file to use (including the file type)"
file_name = gets.to_s
puts ""#{file_name}""
得到的输出是
"test1.txt
"
任何关于什么可能出错的想法都是非常感谢的。谢谢你!
这与您添加的引号无关,而是您没有使用String#chomp来删除每个gets
:
gets # I type foo
# => "foon"
gets.chomp # I type foo
# => "foo"
这里实际上不需要to_s
,因为gets
总是返回一个字符串。