Talks

Whimsy: Pry Irresponsibly

Whimsy: Pry Irresponsibly

by Riaz Virani

In the video titled "Whimsy: Pry Irresponsibly" presented by Riaz Virani at RubyConf 2021, the focus is on demonstrating the powerful features of the Pry gem in Ruby, which serves as an alternative to IRB (Interactive Ruby Shell). Riaz aims to show how developers can streamline their coding process through real-time code execution and manipulation while explaining the inherent risks of such approaches.

Key Points Discussed:

- Introduction to Pry: Riaz introduces Pry as a valuable tool for Ruby developers, highlighting its capabilities beyond standard IRB, particularly in interactive coding.

- Efficiency in Coding: The central idea of the talk is improving coding efficiency by allowing developers to write code while it is executing, reducing the step from writing code and running it into a seamless process.

- Examples of Usage: Riaz demonstrates various features of Pry, including:

- Creating a new class directly in the Pry console.

- Using special commands to navigate between contexts and inspect objects.

- Editing and manipulating class definitions through Pry, showcasing an interactive approach to coding.

- Humorous Illustrations: Throughout the demonstration, Riaz uses humor and a metaphorical narrative about having a 'friend' (the Friend class) to explain the coding process and the changes made to the class.

- Error Handling: He illustrates how to inspect errors and exceptions within the Pry context, focusing on the command .wtf for examining runtime errors and stack traces.

- Monkey Patching: Riaz explains monkey patching directly in Pry, highlighting the ability to alter classes on the fly without needing to restart the process.

- Conclusion: The talk wraps up with Riaz achieving his goal of reclaiming his 'dignity'—a clever metaphor for successfully manipulating his class back to form, while cautioning that while these techniques can be fun and useful, they should be approached with caution in real-world applications due to their potential for introducing bugs or confusion in code bases.

Overall, Riaz's session highlights the playful side of programming with Pry and encourages developers to experiment with its features, while maintaining awareness of best practices in coding.

00:00:00.240 Welcome everyone! This talk is titled "Pry Irresponsibly." For those of you who know, Pry is a Ruby gem, a common alternative to Interactive Ruby (IRB). While IRB provides an interactive console experience, Pry offers additional capabilities. In this talk, we're going to explore some of the things you can do in Ruby using Pry.
00:00:02.560 My name is Riaz Virani, and I'm a freelance web developer. I lived in Atlanta for a while, where I had a company called Load Up Technologies. I later moved to Toronto. If you would like to learn more about my work or watch my talks, you can visit my website at riazv.net.
00:00:43.680 Before we dive into the rest of the talk, I want to clarify that what I'm going to show you is not necessarily things you should do regularly. This is somewhat of an anti-talk. While the information can be helpful and insightful, there are very few use cases where I would actually recommend using any of this in a real application.
00:01:06.240 Let’s talk about a common problem: finishing your work. Often, the issue is that you’re doing too many steps and wasting time writing code and then running it. Consider all the time wasted on these two actions. I have an amazing solution for you: what if you could write your code while it's running? Using Pry, you can reduce your steps from two to one, allowing you to code much faster.
00:01:37.520 Now, let’s see how we can achieve that. I have a terminal here—let me make it a bit larger for you. The directory we're in doesn't have much; it's just got a Gemfile with the Pry gem, which we'll use for our little exercise. We'll start Pry, and since we have no program or anything set up yet, let me also pull this up from the bottom a bit.
00:02:05.360 Once Pry is running, I realize I need a friend, so I will create a friend class. This will allow me to instantiate friends whenever I like. I created the class and, importantly, I can run commands found in the terminal itself. Whenever I use a dot in front of any command in Pry, it allows me to execute terminal commands from within Pry.
00:02:27.760 Inside this context, I will define the friend class with an initialize method. To illustrate, I’ll humorously state that my friends have a tendency of taking my dignity when they want to be associated with me. But, as a programmer, I have to cope with this and continue.
00:03:06.560 Now that I've saved this, I can run a command to list the files, confirming we have the required file. I can create a new friend instance, but I forgot to save. However, Pry allows you to reference the output of the last execution using the underscore. So now, I have a local reference to my friend object.
00:03:40.640 Feeling a bit skeptical about this new friend, I want to look behind the scenes. Pry has a special command that allows me to "cd" into the friend instance, so any command I execute now operates within that context. I can inspect the instance variable holding my dignity. The friend apparently is keeping it safe for the moment.
00:04:04.440 After this realization, I contemplate my friendship and decide I want my dignity back. Using Pry, I can employ an edit command to check the friend class and see if there's a method for returning my dignity. I execute the command to inspect the instance for the necessary methods and variables. Lo and behold, there's now a 'give back' method available.
00:04:50.400 I attempt to call friend.give_back, but it raises a runtime error. I'm left feeling distraught, wondering how my new friend could treat me this way. To investigate further, I use the wtf command, which allows me to view the details of the last exception raised. This command is quite useful and generates a prompt full of insights about the error.
00:06:01.920 When I realize that I need to examine my recent actions, I utilize the hist command to analyze all the previous commands I've executed. This way, I can identify my mistakes. But I remind myself, as the programmer, I still control the situation. Although my friend has caused me some distress, I can reset the conditions.
00:06:52.960 Instead of using the edit command again, I choose to monkey-patch my friend class directly. Paneled with the source code, I set about defining a 'steal_back' method. While writing this, additional commands will allow me to verify that my dignity is once again intact without needing to restart the program.
00:08:06.560 After applying the changes, I can finally restore my dignity. I then update my source code, committing the essential changes. The commits serve as a playful reminder, accompanied by a clever commit message. Following that, I conclude this portion of the talk.
00:09:15.480 And with that, I wrap up the talk. Thank you for participating!