puts, print and p in ruby

I’ve been trying to pick up Ruby and one of the things I was asking myself was the difference in the various ways you can output text.

  • puts: displays text and adds a newline character
  • print: displays text and doesn’t add a newline character (this means you must add newlines manually when using print)
  • p: while it’s similar to puts in that it adds a newline, it calls inspect instead of to_s This is why p can be more useful for debugging since it shows what class an object belongs to.

Screen Shot 2016-02-08 at 19.02.38

More references for those interested

 

Advertisements