Tejas Rawal
Tejas Rawal

Reputation: 41

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? Can I have answer with example please?

Upvotes: 1

Views: 4212

Answers (5)

Prabhakar
Prabhakar

Reputation: 6754

There are two major differences between puts and print in general.

1. New line

puts take every element and prints in a newline(without specifying the need of a new line character "\n" in the end)

Whereas print doesn't print each element in a new line unless the programmer explicitly specify it.

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

print statement prints the empty or NIL values but puts statement doesn't print them if they contain NIL values in it.

> 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"

Upvotes: 1

AMIC MING
AMIC MING

Reputation: 6354

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.

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.

Kernel.puts "Hello, world!"

puts takes only one argument and is rarely followed by other methods or logic, so parentheses are not strictly necessary.

Upvotes: 1

Linuxios
Linuxios

Reputation: 35783

It's simple. puts automatically appends a newline when it prints. print prints the string without modification.

Another difference is in the number of underlying write operations. puts is (roughly) equivalent to:

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

And print (roughly) equivalent to:

STDOUT.write(str)

So, in multithreaded environments, puts can create some weird looking stuff, like this:

Message1Messa
ge2
(blank line)

While printing a string with a concatenated newline yields:

Message1
Message2

Other than that, they're the same.

Upvotes: 2

Mihai8
Mihai8

Reputation: 3147

A comparison can be see in print vs put. For example take a look on Input & output in Ruby.

Upvotes: 0

Aniket Inge
Aniket Inge

Reputation: 25695

print does not add a newline at the end.. puts does.

Most other languages have similar structures too.

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

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

Pascal had Writeln() and Write()

Upvotes: 2

Related Questions