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:
puts1 writes toSTDOUTand adds a newline character unless we are printing aStringthat ends with a newline.p2 writes toSTDOUTusinginspect.pp3 writes toSTDOUTusinginspectandprettyprint.p!4 writes the value and the expression toSTDOUT.
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