简体   繁体   English

带有示例的Ruby中print和puts有什么区别?

[英]What is the differences between print and puts in Ruby with example?

May be this is a duplicate question, What is the differences between print and puts in Ruby? 可能这是一个重复的问题,Ruby中的print和puts有什么区别? Can I have answer with example please? 请问我可以举例吗?

print does not add a newline at the end.. puts does. print没有在结尾处添加一个新行.. puts呢。

Most other languages have similar structures too. 大多数其他语言也具有类似的结构。

Java has System.out.println() and System.out.print() Java具有System.out.println()System.out.print()

C# has Console.WriteLine() and Console.Write() C#具有Console.WriteLine()Console.Write()

Pascal had Writeln() and Write() Pascal有Writeln()Write()

It's simple. 这很简单。 puts automatically appends a newline when it prints. puts在打印时会自动附加换行符。 print prints the string without modification. print打印字符串而无需修改。

Another difference is in the number of underlying write operations. 另一个区别是基础写操作的数量。 puts is (roughly) equivalent to: puts (大致)等于:

STDOUT.write(str)
STDOUT.write("\n")

And print (roughly) equivalent to: print (大致)相当于:

STDOUT.write(str)

So, in multithreaded environments, puts can create some weird looking stuff, like this: 因此,在多线程环境中, puts可能会创建一些看起来很奇怪的东西,如下所示:

Message1Messa
ge2
(blank line)

While print ing a string with a concatenated newline yields: print带有串联换行符的字符串时,将产生:

Message1
Message2

Other than that, they're the same. 除此之外,它们是相同的。

irb(main):014:0> class Person
irb(main):015:1>  attr_accessor :name, :age, :gender
irb(main):016:1> end
=> nil
irb(main):017:0> person = Person.new
=> #<Person:0x2bf03e0>
irb(main):018:0> person.name = "Robert"
=> "Robert"
irb(main):019:0> person.age = 52
=> 52
irb(main):020:0> person.gender = "male"
=> "male"

irb(main):021:0> puts person
#<Person:0x2bf03e0>
=> nil
irb(main):022:0> print person
#<Person:0x2bf03e0>=> nil
irb(main):023:0> print person.name
Robert=> nil
irb(main):024:0> puts person.name
Robert
=> nil

The difference between print and puts is that puts automatically moves the output cursor to the next line (that is, it adds a newline character to start a new line unless the string already ends with a newline), whereas print continues printing text onto the same line as the previous time. print和puts之间的区别是puts自动将输出光标移动到下一行(也就是说,它将添加换行符以开始新行,除非字符串已经以换行符结尾),而print则继续在同一行上打印文本行作为上次。

puts isn't prefixed by the name of a class or object upon which to complete the method and puts is a method made available from the Kernel module and that is included and searched by default, so usually you won't need to use Kernel.puts to refer to it. puts不会以要在其上完成该方法的类或对象的名称为前缀,而puts是可从Kernel模块获得的一种方法,并且默认情况下会包含并搜索该方法,因此通常不需要使用Kernel。放在引用它。

Kernel.puts "Hello, world!"

puts takes only one argument and is rarely followed by other methods or logic, so parentheses are not strictly necessary. puts仅接受一个参数,而很少跟随其他方法或逻辑,因此括号不是严格必需的。

There are two major differences between puts and print in general. 通常,认沽权证和印刷权证之间有两个主要区别。

1. New line 1.新行

puts take every element and prints in a newline(without specifying the need of a new line character "\\n" in the end) puts所有元素并在换行符中打印(最后不指定需要换行符"\\n"

Whereas print doesn't print each element in a new line unless the programmer explicitly specify it. 除非程序员明确指定,否则print不会在换行中打印每个元素。

puts "Hello, Welcome to Ruby"

Output: 输出:

 Hello, Welcome to Ruby
 Dell-System-XPS:~/Documents/2016RoR/Ruby$



print "Hello, Welcome to Ruby"    

Output: 输出:

 Hello, Welcome to RubyDell-System-XPS:~/Documents/2016RoR/Ruby$

Did you notice, there is no new line after the output. 您是否注意到,输出之后没有新行。 However, the new line should work when you explicitly mention the new line character like below 但是,当您明确提及新行字符时,新行应该可以工作,如下所示

print "Hello, Welcome to Ruby \n"

Output: 输出:

Hello, Welcome to Ruby
Dell-System-XPS:~/Documents/2016RoR/Ruby$

2. Empty characters or NIL values 2.空字符或NIL值

print statement prints the empty or NIL values but puts statement doesn't print them if they contain NIL values in it. print语句打印空值或NIL值,但如果puts语句中包含NIL值,则puts语句不打印它们。

> print [nil, 33,44,55]
> [nil, 33, 44, 55] => nil 


 > puts [nil, 33,44,55]
 >  33
 >  44
 >  55
 => nil 

"You see the difference, there is no NIL value printed while using puts " “您会看到差异,使用puts不会打印NIL值”

A comparison can be see in print vs put . 可以在印刷版与印刷版中进行比较 For example take a look on Input & output in Ruby . 例如,看看Ruby中的Input&output

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

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