Pry Into Your Fears

What do you fear more than fear itself? What is fear? Why does it exist?

Our brains are arguably the most complex machines known to man, and while many of its workings remain a mystery, there are some parts of it we understand.

Our brains are efficient and its primary goal is surviving, reproducing and using the least amount of energy possible to do so. To further its efforts of survival, our brains have a nifty reward center in which it rewards its host (ya, “you”) for doing things it deems fit for its grand plan of survival.

Our brains also created and inherited fears which work to keep our bodies on the path of safe travels.

Oh, I forgot to mention, this is actually an article about coding. How, you may wonder?

If you are a beginner to code, or remember what your amateur days felt like, you may remember the constant feelings of fear, and reward. One minute you are the smartest person in the world because you figured out how to solve a problem you were working on, and the next you feel like the utmost stupidest person in the world because, well, you forgot how to solve the next one.

The battle is a tough one and its really hard to learn an entirely new language and logical structure when our brains are constantly bombarding us with subconscious thoughts such as “This is not important!” and “Why are you wasting energy on this, go back to what you know!” and its all time favorite “YOU CAN’T DO THIS!”. But what if I told you there was a way to both change what your brain fears, and what it rewards you for?

Introducing Pry. A Ruby tool for hacking our brains fear and reward system (also a huge time saver…)

So, What is Pry and how does it work?

Pry is a tool that allows us to literally “pry” into our code to see what it is doing, and what will be done to it before it’s actually run. Lets give a super simple example:

Say you are tasked with looping through an array of cities and making each word capitalized.

cities = ["new york", "new jersey"]

This may seem a bit tricky at first, and creating, running and changing methods in the dark will take time, but let’s see what it will look like if we use pry. To run Pry, you need to first install the gem (see here) and you need to require pry on the top of every file you want to use it in. like this

require 'pry'
cities = ["new york", "new jersey"]
def array_captialize(array)
updated_cities = array.map do |city|
city.split.map(&:capitalize).join(" ")
end
end

Now, I saved you the time and built an already working method, however, what if we weren't sure if this was working, or wanted to see what was really happening inside of it. Well, all we got to do is throw in our Pry and whatever line it is at, it will pause your method so you can see what is going on before your method finishes running. Lets check it out

require 'pry'
cities = ["new york", "new jersey"]
def array_captialize(array)
array.map! do |city|
updated_cities = city.split.map(&:capitalize!).join(" ")
binding.pry
end
end

Now, when we call the file in our terminal, this is what we get:

notice where the => is

Whats happening here? Pry, stopped our method from running, in our case assigning updated_cities to every every index it loops from our original array. So, if our Pry stopped our method from running after the first time, what do you think the return value of updated_cities would be at this point in time?

YUP!! we got “New York”. Why? Because our loop only ran once before Pry stopped it in it’s place.

So, if I pressed “next” in the terminal and continued the loop Pry will now stop it a second time and we would get: New Jersey!

You can even play around with the value of updated_cities right in your terminal. Lets say you were adding a condition to this method that only ran if the size of the given index of updated_cities reached a certain amount. To save you the time of seeing what the index size will be, you can just test it from your terminal

we got 10!

As you can see, we got 10 when asking what the size of the current index is. You can play around and manipulate whatever value your Pry is holding at right in the terminal before spending time creating more methods while not being sure if they will work correctly.

We are scared of what we don’t understand, so it’s natural to “fear code”. With a tool like Pry, which allows us to really see whats going on with our code, we can spend more time learning, understanding and building good code. And if you use Pry to build code by understanding exactly what is happening at every crucial point, not only will you break through your fear of the unknown, but even better, you will finally experience the “coders high”, which is you telling your brain to reward you for something you deem important.

P.S. This was a oversimplified and super simple example of what Pry can do, but hopefully I got you curios enough about Pry for you to start using it for your code.