never_had_a_name
never_had_a_name

Reputation: 93206

Add a new line in 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 the string is inserted?

Upvotes: 114

Views: 91027

Answers (3)

lazylead
lazylead

Reputation: 1989

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

file.write "text#{$/}"

where $/ represents OS-dependent new line separator.

Upvotes: 3

maletor
maletor

Reputation: 7122

Use IO#puts.

file.puts @string

Upvotes: 199

Borealid
Borealid

Reputation: 98469

file.write "\n"

Upvotes: 54

Related Questions