Printing

Let’s start with a brief word about printing values. Throughout the notebook, we’ll be printing values out to demonstrate what a certain piece of code does and there a few different ways we can do this:

  1. puts1 writes to STDOUT and adds a newline character unless we are printing a String that ends with a newline.
  2. p2 writes to STDOUT using inspect.
  3. pp3 writes to STDOUT using inspect and prettyprint.
  4. p!4 writes the value and the expression to STDOUT.

See printing in action:

name = "John"

puts name # => John
p name    # => "John"
pp name   # => "John"
p! name   # => name # => "John"

Another useful method is the typeof method which returns the type of the object we pass to it.

We’ll use this method in our print calls to see the type we’re working with like so:

name = "John"
p! name, typeof(name)
#OUTPUT
name         # => "John"
typeof(name) # => String