DEV Community

Andressa
Andressa

Posted on

5 Different methods to print data in Ruby

Quick index

  1. print vs puts
    1. puts
    2. print
  2. p vs pp vs ap
    1. p
    2. pp
    3. ap
  3. How they actually work
  4. References

print vs puts

First thing to notice, they print the content, but both return nil.

puts

  • Appends a new line at the end of the return
  • Attempts to convert everything into a string by calling the to_s method
irb(main):002:0> puts "take a look at where the cursor is :) "
take a look at where the cursor is :) 
=> nil                                  

irb(main):004:0> 2.times {puts "hey you!"}
hey you!
hey you!                                                                        
=> 2     

# Puts converts everything it cans to string
irb(main):031:0> puts [1,nil,nil,2]
1


2
=> nill             
Enter fullscreen mode Exit fullscreen mode

print

  • no new lines appended here
irb(main):001:0> print "you should not see a new line at the end"
you should not see a new line at the end => nil

irb(main):003:0> 2.times {print "hey you!"}
hey you!hey you! => 2

print [1,nil,nil,2]
[1, nil, nil, 2] => nil

Enter fullscreen mode Exit fullscreen mode

p vs pp vs ap

It is more useful for debugging as it will show ( return ) a raw version of the object.

They are similar to puts as it adds a newline at the end, but instead of calling to_s:

p

  • calls the inspect method.
irb(main):033:0> p 1
1
=> 1                                             
irb(main):034:0> p '1'
"1"
=> "1"                                           
irb(main):035:0> 
Enter fullscreen mode Exit fullscreen mode
irb(main):035:0> puts 1
1
=> nil                                           
irb(main):036:0> puts '1'
1
=> nil                                           
irb(main):037:0> puts '1'.inspect
"1"
=> nil 

Enter fullscreen mode Exit fullscreen mode

pp

  • calls the pretty_inspect method. More useful when dealing with complex nested objects.
irb(main):038:0> data = [ false, 42, %w(forty two), { :now => Time.now, :class =
> Time.now.class, :distance => 42e42 } ]
=> 
[false,                                                                         
...                                                                             
irb(main):039:0> pp data
[false,
 42,                                                                            
 ["forty", "two"],                                                              
 {:now=>2022-06-09 12:06:44.161486 -0400, :class=>Time, :distance=>4.2e+43}]    
=>                                                                              
[false,                                                                         
 42,                                                                            
 ["forty", "two"],                                                              
 {:now=>2022-06-09 12:06:44.161486 -0400, :class=>Time, :distance=>4.2e+43}]    
irb(main):040:0> 
Enter fullscreen mode Exit fullscreen mode

ap

  • Most recently version of Ruby does not contain ap [at the time of writing this article, ruby 3.1.2], you will have to install it by running:
gem install awesome_print
Enter fullscreen mode Exit fullscreen mode

and will have to add require "awesome_print" into your irb session.

irb(main):002:0> require "awesome_print"
=> true
irb(main):003:0> data = [ false, 42, %w(forty two), { :now => Time.now, :class =
> Time.now.class, :distance => 42e42 } ]
=> 
[false,                                 
...                                     
irb(main):004:0> ap data
[
    [0] false,                             
    [1] 42,                                
    [2] [                                  
        [0] "forty",                       
        [1] "two"                          
    ],                                     
    [3] {                                  
             :now => 2022-06-09 12:56:59.184906 -0400,
           :class => Time < Object,
        :distance => 4.2e+43
    }
]
=> nil

Enter fullscreen mode Exit fullscreen mode

How they actually work

They all use the $stdout global variable. It represents the current standard output.

Those methods we have talked about today, communicate with the output stream on your computer (which will communicate with the Operating System) by sending the message using the $stdout global variable, and output the message to the console.


References

Top comments (0)