在字符串周围添加双引号将在新行添加结束引号



我刚开始使用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总是返回一个字符串。

最新更新