#puts vs. print vs. p ###The 'puts' (short for "put string") and 'print' commands are both used to display the results of evaluating Ruby code. ###Both 'puts' and 'print' call the 'to_s' method on the object AND return nil.
###The primary difference between them is that 'puts' adds a newline after executing, and 'print' does not. ###They don't RETURN anything so the RETURN value is nil. ###Using 'p' calls the 'inspect' method on the object.
print "Milan"
Milan => nil
puts "Milan"
Milan
=> nil
p "Milan"
"Milan"
=> "Milan"
3.times { print "Hello!" }
Hello!Hello!Hello!=> 3
3.times { puts "Hello!" }
Hello!
Hello!
Hello!
=> 3
3.times { p "Hello!" }
"Hello!"
"Hello!"
"Hello!"
=> 3
Hi, I added some spaces and enter and made the text preview easier to read.
puts
vs.print
vs.p
The
puts
(short for "put string") andprint
commands are both used to display the results of evaluating Ruby code.Both
puts
andprint
call theto_s
method on the object AND return nil.The primary difference between them is that
puts
adds a newline after executing, andprint
does notThey don`t RETURN anything so the RETURN value is nil.
Using
p
calls theinspect
method on the object.