简体   繁体   中英

How do i create line breaks in ruby?

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

print "Hi"
print "Hi"

Because it would just output this:

HiHi

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.

Use line break character:

print "Hi\n"
print "Hi"

You can create a space by adding a string with only a space in it between the 2 other strings. For example:

print "Hi" + " " + "Hi"

You could avoid the two print statements and instead only use one line.

print "Hi\r\nHi"

Or if you want to use two lines then

print "Hi\r\n"

print "Hi"

put“\\ n”也适用于Win / Ruby ruby​​ 2.4.2p198甚至“\\ n”* 4用于新行的乘法(乘以4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM