如何在 ruby 中创建换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14696944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i create line breaks in ruby?
提问by Julian Boaz
How would i put line breaks in between lines like this:
我将如何在这样的行之间放置换行符:
print "Hi"
print "Hi"
Because it would just output this:
因为它只会输出这个:
HiHi
回答by maerics
Use putssince it will automatically add a newline for you:
使用,puts因为它会自动为您添加换行符:
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.
回答by Alexander Zinchenko
Use line break character:
使用换行符:
print "Hi\n"
print "Hi"
回答by helloJello
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"
回答by Stefan Hagen
You can create a space by adding a string with only a space in it between the 2 other strings. For example:
您可以通过在其他 2 个字符串之间添加一个只有一个空格的字符串来创建一个空格。例如:
print "Hi" + " " + "Hi"
回答by PBesze
puts "\n" works also on Win/Ruby ruby 2.4.2p198 and even "\n"*4 for multiplication of new rows (by 4)
puts "\n" 也适用于 Win/Ruby ruby 2.4.2p198 甚至 "\n"*4 用于新行的乘法(乘以 4)

