You got a friend in me — Rubyist’s beginner tool

Vmanzanilla
4 min readAug 13, 2020
Buzz(Pry) saves woody(developer). Source: https://www.pinterest.com/pin/490962796848130000/
“…When the road looks rough ahead
And you’re miles and miles
From your nice warm bed…” — Randy Newman

Intro

Pry is a software that you can install to help you debug your code. It is an interactive shell( a shell is like the bridge from user to machine) for the Ruby Programming Language. This is a great tool to have as a Junior developer because it will help you see what will happen if you execute your code. One day you feel confident about the output of your code, but only to an Error, as developers, we have to embrace those Errors. As a Software Engineer in training, I will do my best to explain how to use pry and other useful tips.

source: https://www.google.com/search?q=neo%20stops%20time&tbm=isch&tbs=itp%3Aanimated&hl=en&sa=X&ved=0CAMQpwVqFwoTCKDyquKfl-

Pry is like Neo that stops the bullets, bullets is the code executing, the enemy is the potential errors. Pry will stop or freeze your code then you can go inside and see the values that will return and fix it.

Example!

My program is running a method called “walks”. The method starts at “def walks” and ends(I close it) at “end”(the last “end”). If I wanted to know the value of “Walk” and “walk_instance” in…

def walks
Walk.all.select do |walk_instance|

Then I would insert “binding.pry” underneath.

def walks
Walk.all.select do |walk_instance|
binding.pry

Then I would run my code(to run you have to call on the method first) and on the terminal, I would see…

27: def walks
28: Walk.all.select do |walk_instance|
=> 29: binding.pry
30: walk_instance.dog == self
31: end
32: end

My code would run and stop at line 29 because of my binding.pry. Then on the terminal, I type “walk_instance”

[1] pry(#<Dog>)> walk_instance

and get…

[1] pry(#<Dog>)> walk_instance
=> #<Walk:0x00007fe772b4deb0
@dog=#<Dog:0x00007fe772b4e2c0 @breed="German shepherd", @good_dog="Good", @name="El aleman">,
@dog_walker=#<DogWalker:0x00007fe772b4dfc8 @favorite_breed="German shepherd", @name="Baguetteboi">,
@length_in_minutes=60>

From here I can see how I can get dog, so I can compare it to self. What this method does is returns an array of all the walks that the Dog has taken.

Now some useful tips that Pry can do for you!

source: https://www.google.com/search?q=buzz%20saves%20woody&tbm=isch&tbs=itp%3Aanimated&hl=en&sa=X&ved=0CAMQpwVqFwoTCOj49t2l

As you are learning to work with Ruby, you will need to learn about Array and Hash methods to help manipulate them. The most common methods that you will learn to use are .select, .map, .find, and of course .each. As you learn to use these methods, you will find yourself looking it up again and again. Well, Pry has a way to show how an Array and hash methods work right on your terminal. Like so…

https://www.rubydoc.info/gems/pry-docsource:

Here is the link to learn more about installing.

A few Pry commands to know

* binding.pry — Stops or freezes the program and opens pry console

* exit — Exits current loop

* exit! or !!! — Exits Pry console

* whereami — Prints current location within program

* show-doc — Displays documentation for a class methods

* ls — Prints the variables and methods in the current scope of the program

* help — Prints the current list of available Pry commands

I would say the top three are a must learn.

Installing

You would need to install the gem file:

gem install pry

Next, I have this written on top of your code editor file:

require 'pry'

Then you are good to go! I hope you found this useful. I know found pry to be very useful on my way to learn how to code. Thank you for reading!

--

--