5 Different methods to print data in Ruby
Tags: ruby, programming, codenewbie, beginners
Published: 2022-06-10 01:39:44

**_Quick index_**

1. [print vs puts](#print-vs-puts)
  1. [puts](#puts)
  2. [print](#print)
2. [p vs pp vs ap](#p-vs-pp-vs-ap)
  1. [p](#p)
  2. [pp](#pp)
  3. [ap](#ap)
3. [How they actually work](#how-they-actually-work)
4. [References](#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

```ruby
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             
```

###print 
- no new lines appended here 

```ruby
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

```

## 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. 

```ruby 
irb(main):033:0> p 1
1
=> 1                                             
irb(main):034:0> p '1'
"1"
=> "1"                                           
irb(main):035:0> 
```

```ruby 
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 

```

###pp
- calls the `pretty_inspect` method. More useful when dealing with complex nested objects.

```ruby
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> 
```

###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: 

```ruby 
gem install awesome_print
```

and will have to add `require "awesome_print"` into your **_irb_** session.
 
```ruby
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

```

## 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
+ [Module Kernel Doc](https://ruby-doc.org/core-3.1.2/Kernel.html)
+ [Puts, Print and Return](https://learn.co/lessons/puts-print-and-return-readme)
+ [Puts vs Print](https://www.rubyguides.com/2018/10/puts-vs-print/)
+ [Awesome Print Repository](https://github.com/awesome-print/awesome_print)