#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.putsvs.printvs.pThe
puts(short for "put string") andprintcommands are both used to display the results of evaluating Ruby code.Both
putsandprintcall theto_smethod on the object AND return nil.The primary difference between them is that
putsadds a newline after executing, andprintdoes notThey don`t RETURN anything so the RETURN value is nil.
Using
pcalls theinspectmethod on the object.