ruby - 在字符串周围添加双引号会在新行上添加右引号
问题描述
我刚开始使用 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删除包含在every gets
中的尾随换行符:
gets # I type foo
# => "foo\n"
gets.chomp # I type foo
# => "foo"
你实际上并不需要to_s
这里,因为它gets
总是会返回一个字符串。
推荐阅读
- python - 为什么主进程和子进程在主进程异常后不退出?
- python - 多次写入csv文件?
- r - R tibble: get column name of first column for referencing
- javascript - Javascript - 检测窗口是否处于非活动状态但仍然可见
- unity3d - 重新导入资产后,Android 的统一构建设置不可更改
- python - 如何归档“无效请求[0].updateTextStyle:必须在“字段”中列出至少一个字段。(使用 '*' 表示所有字段。)">'
- python - 对于虚拟 conda 环境,PyCharm 不工作,而 Jupyter notebook 工作
- reactjs - React useState 等待状态被设置
- php - PHP 文件下载:PHP 正在 ajax 响应中发送文件数据,但文件未下载
- javascript - 如何修复 _this.video 未定义