Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i create line breaks in ruby?

Tags:

ruby

How would i put line breaks in between lines like this:

print "Hi"
print "Hi"

Because it would just output this:

HiHi
like image 251
Julian Boaz Avatar asked Feb 04 '13 22:02

Julian Boaz


People also ask

How do you make a new line in Ruby?

"\n" is newline, '\n\ is literally backslash and n.

How do you break the long line in Ruby?

Use the backslash for Writing Multi-Line String in Ruby.

How do you add a line break in string?

Inserting a newline code \n , \r\n into a string will result in a line break at that location. On Unix, including Mac, \n (LF) is often used, and on Windows, \r\n (CR + LF) is often used as a newline code.

What is a line break in code?

In Windows and DOS, the line break code is two characters: a carriage return followed by a line feed (CR/LF). In the Unix/Linux/Mac world, the code is just the line feed character (LF).


2 Answers

Use puts since it will automatically add a newline for you:

puts "Hi"
puts "Hi"

If you want to make an explicit newline character then you'll need to know what kind of system(s) on which your program will run:

print "Hi\n"   # For UNIX-like systems including Mac OS X.
print "Hi\r\n" # For Windows.
like image 73
maerics Avatar answered Nov 06 '22 17:11

maerics


Use line break character:

print "Hi\n"
print "Hi"
like image 24
Alexander Zinchenko Avatar answered Nov 06 '22 18:11

Alexander Zinchenko