print 和 puts 和有什么不一样? [英] What is the difference between print and puts?
本文介绍了print 和 puts 和有什么不一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如在我写的这行代码中,print
和 puts
产生不同的结果.
1.upto(1000).each { |i|如果 i % 2 == 0 则打印 i }
解决方案
puts
如果每个参数还没有,则在每个参数的末尾添加一个新行.
print
不添加新行.
例如:
puts [[1,2,3], [4,5,nil]]
会返回:
而 打印 [[1,2,3], [4,5,nil]]
会返回:
[[1,2,3], [4,5,nil]]
注意 puts 不输出 nil 值而 print 输出.
For example in this line of code I wrote, print
and puts
produce different results.
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.
print
does not add a new line.
For example:
puts [[1,2,3], [4,5,nil]]
Would return:
1 2 3 4 5
Whereas print [[1,2,3], [4,5,nil]]
would return:
[[1,2,3], [4,5,nil]]
Notice how puts does not output the nil value whereas print does.
这篇关于print 和 puts 和有什么不一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文