Yes, that sounds disrespectful!
Well, I am living my first weeks of programming at Flatiron Nyc!
I wanted my first subject to deal with machine learning and model training. However, one of the guidelines was to speak about something that troubled you. Well Pry did. I felt really mistreated at the beginning… But now I start to feel the love, little by little.
So yes, I am changing career by deciding to go deeper into programming, and guess what? I understood that « the job is about being stuck dear friend and enjoying unstucking yourself ». Well, I got stuck, and was excited to find a solution! In the early stages, I debugged myself by changing several times my code with my espresso machine’s indefectible support! I made my way out… until a certain level.
After several days of completing the joy of immobilization, we were introduced to Pry.
Pry is your friend.
It was not love at the first sight. Then, with practice and time, I slowly understood the magic. First through the concept, then by being stuck (again) on « stage ». The honest truth is that you have to practice a lot, ask questions, harass your peers, give some hugs and stop forgetting about:
– Requiring it at the top of your file
– Calling « binding.pry » exactly where you want to inspect: a return value, a code.
– Calling the method before running the code. I know it does sound obvious, but I froze few times because of it.
– Avoiding to hard exit with three bangs to see the return value of a loop, and simply exit as needed.
“Errors are Your Friends”
Pry will just « pry » in the scope where it is called. It is not because you are in the « Tweet » class that you will access all your tweets from any all the areas of your text editor window. You will definitely need to pry in the scope where variables or methods you want to look up to, are accessible. Also, you must exit the pry each time when changes are made in the code.
Oh! and keep an eye on which type of method you need to call it on. You want to make sure you remember from where you did « pry »: Are you in an instance method or in a class method? This could offer you some error « treats ». And you better install the Gem or you could keep speaking to your terminal with nothing happening forever!
To be more specific
Calling binding. pry it is searching into the current state of the code, from outside your file. When you write binding.pry in your code, that line gets interpreted as the program runs. Moreover, it could be used as a sandbox where you could test code, like IRB, but way better!
In other words
Imagine going out to a party. On your way back to your car you realize that you don’t have your car keys!
Well, if Pry was an app that could freeze time in real life:
You could put some binding.pry at different stages of your evening, freeze the past hours until the moment of your choosing. You will happily start looking for your keys, check in each room, and check in other places you passed by until now. If that was possible in real life, we will be using this app all the time right !?
I hope that you found some light in here because you will want to master this powerful tool: «Pry my word !»