Difference between p, print and puts in ruby

Difference between p, print and puts in ruby is something which a new ruby learner comes across with.

But I have seen many times an expert doesn’t feel very comfortable or able to tell you the exact difference these three.

In simple words -

print and puts return nil. While p return whatever it is given in print.

a = puts "Hello"   #(prints line and takes the control to next line.)
b = print "Hello"  #(prints line and takes the control to the end of the line.)

#=> Here value of a and b would be nil. 

c = p "Hello"      #(prints line and takes the control to next line.)

#=> Here value of c would be "Hello".
comments powered by Disqus