The Post
Ruby Basics: p vs puts
Setting up the file:
Put the following code into a .rb
file:
Now run that file. Nothing will happen. That’s because while we’ve assigned some variables, we haven’t told it to do anything with those variables.
p
statements
Add on to the file:
Output should look like:
Now change the code so it looks like this:
Output should look exactly the same as before. This is because p
statements can still return a value, even when they are before a variable assignment. Puts statements do the same thing, but with some differences, which we’ll get to.
Change the code back to look like this:
puts
statements
Add in some puts
statements now:
What you should see:
Notice something? When a p
statement runs, it returns something very important. It gives information regarding the data type of the thing being given.
Very important if you run this:
Which returns this:
Very important when you’re tracking a bug down. So many problems arise from a datatype error. If we’re trying to do math, but one of our numbers is a string, the program will break. If we just used puts
statements, you’d never see it, because puts
doesn’t give us a datatype when printing strings. It would just look like:
Which is only half the problem. This is also why we p
parts of the code we think are broken, rather than puts
.
The difference between p
and puts
is in their intent. If we want to give information, with no regard to the datatype (like error messages), then puts
is a good start. But if we’re debugging, when in doubt, p
it out. We need as much info as possible.