Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a new line in file?

Tags:

ruby

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?

like image 979
never_had_a_name Avatar asked Aug 19 '10 02:08

never_had_a_name


People also ask

How do I add a new line to a file?

Append Text Using >> Operator Alternatively, you can use the printf command (do not forget to use \n character to add the next line). You can also use the cat command to concatenate text from one or more files and append it to another file.

Does \n make a new line?

Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.

How do you add a new line of text to an existing file in Java?

append("New Line!"); output.

How do you enter a new line in a text file in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.


2 Answers

Use IO#puts.

file.puts @string 
like image 143
maletor Avatar answered Oct 11 '22 14:10

maletor


file.write "\n"

like image 26
Borealid Avatar answered Oct 11 '22 13:10

Borealid