标签: ruby
我想在插入字符串后添加新行。
我目前的代码如下:
File.open(filename, 'a') do |file| file.write @string end
如何在插入字符串后添加新行?
答案 0 :(得分:158)
使用IO#puts。
file.puts @string
答案 1 :(得分:41)
file.write "\n"
答案 2 :(得分:0)
如果您不能使用 IO#puts:
file.write "text#{$/}"
其中 $/ 表示依赖于操作系统的换行符。
$/