简体   繁体   English

print和puts有什么区别?

[英]What is the difference between print and puts?

例如,在我编写的这行代码中, printputs产生不同的结果。

1.upto(1000).each { |i| print i if i % 2 == 0 }

puts adds a new line to the end of each argument if there is not one already. 如果没有一个参数,则puts会在每个参数的末尾添加一个新行。

print does not add a new line. print不会添加新行。


For example: 例如:

puts [[1,2,3], [4,5,nil]] Would return: puts [[1,2,3], [4,5,nil]]将返回:

1
2
3
4
5

Whereas print [[1,2,3], [4,5,nil]] would return: print [[1,2,3], [4,5,nil]]将返回:

[[1,2,3], [4,5,nil]]
Notice how puts does not output the nil value whereas print does. 请注意puts如何输出nil值而print不会。

A big difference is if you are displaying arrays. 一个很大的区别是,如果您正在显示数组。 Especially ones with NIL. 特别是那些有NIL的人。 For example: 例如:

print [nil, 1, 2]

gives

[nil, 1, 2]

but

puts [nil, 1, 2]

gives

1
2

Note, no appearing nil item (just a blank line) and each item on a different line. 注意,没有出现零项(只是一个空行)和不同行上的每个项目。

print outputs each argument, followed by $, , to $stdout , followed by $\\ . print输出每个参数,后跟$, ,到$stdout ,然后是$\\ It is equivalent to args.join($,) + $\\ 它相当于args.join($,) + $\\

puts sets both $, and $\\ to "\\n" and then does the same thing as print . puts $,$\\为“\\ n”,然后执行与print相同的操作。 The key difference being that each argument is a new line with puts . 关键的区别在于每个参数都是带有puts的新行。

You can require 'english' to access those global variables with user-friendly names . 您可以require 'english'用户友好的名称访问这些全局变量。

The API docs give some good hints: API文档提供了一些很好的提示:

print() → nil

print(obj, ...) → nil

Writes the given object(s) to ios . 将给定对象写入ios Returns nil . 返回nil

The stream must be opened for writing. 必须打开流才能写入。 Each given object that isn't a string will be converted by calling its to_s method. 每个不是字符串的给定对象将通过调用其to_s方法进行转换。 When called without arguments, prints the contents of $_ . 在不带参数的情况下调用时,打印$_的内容。

If the output field separator ( $, ) is not nil , it is inserted between objects. 如果输出字段分隔符( $, )不是nil ,则将其插入对象之间。 If the output record separator ( $\\ ) is not nil , it is appended to the output. 如果输出记录分隔符( $\\ )不是nil ,则将其附加到输出。

... ...

puts(obj, ...) → nil

Writes the given object(s) to ios . 将给定对象写入ios Writes a newline after any that do not already end with a newline sequence. 在任何尚未以换行符结束的内容之后写入换行符。 Returns nil . 返回nil

The stream must be opened for writing. 必须打开流才能写入。 If called with an array argument, writes each element on a new line. 如果使用数组参数调用,则将每个元素写入新行。 Each given object that isn't a string or array will be converted by calling its to_s method. 每个不是字符串或数组的给定对象将通过调用其to_s方法进行转换。 If called without arguments, outputs a single newline. 如果不带参数调用,则输出一个换行符。

Experimenting a little with the points given above, the differences seem to be: 用上面给出的点进行一些实验,差异似乎是:

  • Called with multiple arguments, print separates them by the 'output field separator' $, (which defaults to nothing) while puts separates them by newlines. 使用多个参数调用时, print将它们分隔为“输出字段分隔符” $, (默认为空),而puts则用换行符分隔它们。 puts also puts a newline after the final argument, while print does not. puts也会在最后一个参数后面添加换行符,而print则不会。

     2.1.3 :001 > print 'hello', 'world' helloworld => nil 2.1.3 :002 > puts 'hello', 'world' hello world => nil 2.1.3 :003 > $, = 'fanodd' => "fanodd" 2.1.3 :004 > print 'hello', 'world' hellofanoddworld => nil 2.1.3 :005 > puts 'hello', 'world' hello world => nil 
  • puts automatically unpacks arrays, while print does not: puts自动解压缩数组,而print则不会:

     2.1.3 :001 > print [1, [2, 3]], [4] 2.1.3:001> 打印[1,[2,3]],[4]\n[1, [2, 3]][4] => nil [1,[2,3]] [4] =>无 \n2.1.3 :002 > puts [1, [2, 3]], [4] 2.1.3:002> 放[1,[2,3]],[4]\n1 1\n2 2\n3 3\n4 4\n => nil =>无 
  • print with no arguments prints $_ (the last thing read by gets ), while puts prints a newline: print不带参数打印$_ (读出的最后一件事gets ),而puts打印新行:

     2.1.3 :001 > gets hello world => "hello world\\n" 2.1.3 :002 > puts => nil 2.1.3 :003 > print hello world => nil 
  • print writes the output record separator $\\ after whatever it prints, while puts ignores this variable: printprint写入输出记录分隔符$\\ ,而puts忽略此变量:

     mark@lunchbox:~$ irb 2.1.3 :001 > $\\ = 'MOOOOOOO!' => "MOOOOOOO!" 2.1.3 :002 > puts "Oink! Baa! Cluck! " Oink! Baa! Cluck! => nil 2.1.3 :003 > print "Oink! Baa! Cluck! " Oink! Baa! Cluck! MOOOOOOO! => nil 

puts call the to_s of each argument and adds a new line to each string, if it does not end with new line. puts调用每个参数的to_s ,并为每个字符串添加一个新行,如果它不以新行结束。 print just output each argument by calling their to_s . print只需调用它们的to_s输出每个参数。

for example: puts "one two" : one two 例如: puts "one two"one two

{new line} {新队}

puts "one two\\n" : one two puts "one two\\n"one two

{new line} #puts will not add a new line to the result, since the string ends with a new line {new line} #puts不会在结果中添加新行,因为字符串以换行结束

print "one two" : one two print "one two"one two

print "one two\\n" : one two print "one two\\n"one two

{new line} {新队}

And there is another way to output: p 还有另一种输出方式: p

For each object, directly writes obj.inspect followed by a newline to the program's standard output. 对于每个对象,直接将obj.inspect后跟换行符写入程序的标准输出。

It is helpful to output debugging message. 输出调试信息很有帮助。 p "aa\\n\\t" : aa\\n\\t p "aa\\n\\t"aa\\n\\t

If you would like to output array within string using puts , you will get the same result as if you were using print : 如果您想使用puts在字符串中输出数组,您将得到与使用print相同的结果:

puts "#{[0, 1, nil]}":
[0, 1, nil]

But if not withing a quoted string then yes. 但如果没有带引号的字符串则是。 The only difference is between new line when we use puts . 唯一的区别是当我们使用puts时新线之间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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