Sharpen your code through Pry
Published: 2020-01-02 20:55:52

Navigating through an unknown territory is frustrating when you don't have the the right tools guide you. As a programmer, I often wrote code aimlessly until I got the right response. It felt good until I dove deeper and deeper into my learning that the knowledge that I've built failed to explain my understanding of the language- Ruby. This isn't the case anymore. And here is an excerpt from a book called *The 7 Habits of Highly Effective People*, that illustrates mindfulness in coding.


>There's a guy who stumbled into a lumberjack in the mountains. The man stops to observe the lumberjack, watching him feverishly sawing at this very large tree. He noticed that the lumberjack was working up a sweat, sawing and sawing, yet going nowhere. The bystander noticed that the saw the lumberjack was using was about as sharp as a butter knife. So, he says to the lumberjack, "Excuse me Mr. Lumberjack, but I couldn't help noticing how hard you are working on that tree, but going nowhere." The lumberjack replies with sweat dripping off of his brow, "Yes... I know. This tree seems to be giving me some trouble." The bystander replies and says, "But Mr. Lumberjack, your saw is so dull that it couldn't possibly cut through anything." "I know", says the lumberjack, 'but I am too busy sawing to take time to sharpen my saw."

>-Stephen R. Covey

Surely, practicing as much as possible will improve your skills the best. But we definitely need to be mindful about how we improve out craft. One way that we can do that is through Pry. 

What is pry? 

Pry is a debugging tool and a Ruby REPL that helps alleviate our headaches. 

Using pry will stop at a specific line of code where you can then evaluate your code in your project. First, your project will need to `require 'pry'`. Install the gem if you already haven't. Then You want to install  `binding.pry` at the breakpoint(the area of code you want to test). 


Here is an example of using `binding.pry` to get the outcome we want:

```ruby 
 def add_numbers_to_arr(numbers_arr, num1, num2)
     # add num1 and num2 to numbers_arr
     numbers_arr << num1
     binding.pry
     numbers_arr = num2
     binding.pry
     numbers_arr
 end

result = add_numbers_to_arr([3,4], 5, 6)
puts result == [3, 4, 5, 6] # why is this false?
```

After running our code in the terminal we get:

```ruby
 3: def add_numbers_to_arr(numbers_arr, num1, num2)
     4:     # add num1 and num2 to numbers_arr
     5:     numbers_arr << num1
 =>  6:     binding.pry
     7:     numbers_arr = num2
     8:     binding.pry
     9:     numbers_arr
    10: 
    11: end

[1] pry(main)> 
```
We enter `numbers_arr` and get:

```ruby
[1] pry(main)> numbers_arr
=> [3, 4, 5]
[2] pry(main)> 
```
Line 5 looks good, but when we enter `numbers_arr` again we get:
```ruby
     3: def add_numbers_to_arr(numbers_arr, num1, num2)
     4:     # add num1 and num2 to numbers_arr
     5:     numbers_arr << num1
     6:     binding.pry
     7:     numbers_arr = num2
 =>  8:     binding.pry
     9:     numbers_arr
    10: 
    11: end

[1] pry(main)>  
```
Here is where we find our issue. We set the value of numbers_arr to num2. We were not able to add `num1` and `num2` together. But this is good! We now know where we need to fix the issue. That is line 7.
```ruby
[1] pry(main)> numbers_arr
=> 6
[2] pry(main)> 
```
Now that we know where the issue is we can change the code to get the outcome. We know shovel `num2` into `numbers_arr`

```ruby
def add_numbers_to_arr(numbers_arr, num1, num2)
    # add num1 and num2 to numbers_arr
    numbers_arr << num1
    binding.pry
    numbers_arr << num2 
    binding.pry
    numbers_arr
   
end
```
We did it! 

```ruby
[1] pry(main)> numbers_arr
=> [3, 4, 5, 6]
```
In conclusion, we have tools to help us know how to navigate our code. Use them so that you're equipped to know your code.