在文件中添加新行?

我想在插入字符串之后添加一个新行。

我当前的代码如下:

  File.open(filename, 'a') do |file|
file.write @string
end

在插入字符串之后,如何添加新行?

85439 次浏览

file.write "\n"

Use IO#puts.

file.puts @string

In case if you can't use IO#puts:

file.write "text#{$/}"

where $/ represents OS-dependent new line separator.