开发者

Add a new line in file?

开发者 https://www.devze.com 2023-01-11 23:47 出处:网络
I want to add a new line after a string is inserted. My current code looks like this: File.open(filename, \'a\') do |file|

I want to add a new line after a string is inserted.

My current code looks like this:

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

How could I add a new line after th开发者_Go百科e string is inserted?


Use IO#puts.

file.puts @string


file.write "\n"


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

file.write "text#{$/}"

where $/ represents OS-dependent new line separator.

0

精彩评论

暂无评论...
验证码 换一张
取 消