RailsConf 2013

Pry-- The Good Parts!

Pry-- The Good Parts!

by Conrad Irwin

In the video titled "Pry-- The Good Parts!" presented by Conrad Irwin at Rails Conf 2013, the focus is on Pry, a powerful development console for Ruby, designed to enhance debugging and code exploration. Irwin emphasizes how essential Pry has become for Ruby and Rails programmers, transforming a simple interactive Ruby shell into a robust tool. The session starts by contrasting Pry with irb, highlighting its improved features:

  • Syntax Highlighting: Pry provides color-coded output, making it easier to read and understand program flow.
  • Introspection Commands: Unique distinct commands, such as ls, that offer an intuitive interface to explore methods available on modules quickly.
  • Binding.pry Feature: A crucial aspect of Pry that allows developers to pause execution in a running program, enabling real-time debugging and inspection of variables.

Irwin illustrates these features using a fictional blog engine example, detailing how Pry aids in solving various programming problems. He discusses several scenarios where Pry's capabilities significantly enhance the coding experience:

  • Exploring Libraries: Demonstrates using the base64 library where he utilizes commands such as ls to find available methods and question mark for quick documentation lookup.
  • Debugging with Binding Pry: Describes placing a binding.pry statement to pause execution and explore variable states without needing to rerun the entire program.
  • Editing Code on the Fly: Shows how to edit methods directly from Pry using the edit command or edit -x for the last raised exception, streamlining debugging processes.

Additionally, Irwin talks about common tasks like producing user-friendly URLs by implementing safe string replacement methods. Throughout the presentation, he reinforces the importance of using Pry over traditional logging methods, arguing that it saves substantial time in debugging because it allows developers to interactively inspect their code.

Finally, the video concludes with Irwin discussing some advanced plugins for Pry, such as Pry-Rails and Pry-Debugger, which further enhance its functionality, emphasizing that addressing debugging early in development can lead to much more efficient coding practices. He reiterates that everything shown is core to Pry and highly beneficial for Ruby developers, encouraging viewers to integrate it into their coding workflows.

Overall, the session delivers crucial insights into how Pry can transform the Ruby programming experience, making it indispensable for efficient debugging.

00:00:16.400 Hi, I'm Conrad, one of the maintainers of Pry. Just before we get started, how many people already know about Pry? Oh wow, that's kind of cool!
00:00:21.600 If you don't know, the easiest way to think of Pry is that it's like IRB but better. Just like you can type Ruby code into IRB and it tells you the answer, you can do the same in Pry, but better. I say it's better for three main reasons.
00:00:34.800 Firstly, we have syntax highlighting. This makes it really easy to read the output of Pry, allowing you to see where you are and what you've been doing.
00:00:47.039 Secondly, we have Pry commands. These commands wrap all of Ruby's really nice introspection APIs in a way that is intuitive and easy to use, helping you quickly grasp the shape of your program.
00:01:04.720 Thirdly, we have 'binding.pry'. This feature is a unique selling point of Pry, allowing you to open a REPL session in the middle of a running program. It helps you debug problems and understand what your code is doing during execution.
00:01:16.960 While those features are fantastic, it can be challenging to visualize the point of using them. I like to think of Pry as everything you need to program in Ruby, apart from a text editor. This is important because developers spend about 40% of their time writing code; the remaining 60% is spent debugging. You need a tool like Pry that can get you into the code and help you understand what's going on.
00:01:37.119 When I mentioned I would be talking about Pry, John Mayer said I had to include him, so here's a little history lesson. In 2010, he was working on a graphics library, trying to debug some problems. Fast forward three years of procrastination later, we now have a debugging library, but no graphics library that I'm aware of.
00:02:04.079 Ryan and I are the other gem maintainers. We have 71 different contributors and around two million downloads of Pry, which is about one-tenth the size of Rails, for a scale comparison.
00:02:30.000 The typical Rails introduction is to show how to write a blog engine. As you've likely seen in various videos, blog engines tend to work right away the first time you write them. I attempted to write a blog engine as well and discovered that it wasn't quite as easy as everyone made it out to be.
00:02:41.360 To discuss Pry, I will showcase some of the challenges I faced while building this completely fictional blog engine and how I used Pry to resolve them. In particular, Pry is designed not to have just a few significant features but instead many small ones that collectively save you time.
00:03:06.480 Thus, it is more effective to describe Pry in terms of several sets of features that work together rather than listing each feature alphabetically.
00:03:20.800 For my blog engine, I had two primary requirements. Firstly, only I should be able to post blog posts, which meant I needed some sort of authentication. As web programmers, we all know about HTTP Basic Authentication.
00:03:32.879 This is actually built into web browsers, but no one uses it because the user interface is really bad. Essentially, the browser will ask me for a username and password, combine them with a colon, Base64 encode them, and send them to me in an HTTP header.
00:03:56.319 This works nicely because Ruby has a Base64 library, which I can use to decode it.
00:04:01.680 However, I often struggle to recall how to use the Base64 library, and that’s where Pry comes in handy to show me how it works. Very simply, I can start Pry just as I would start IRB by typing it into Bash.
00:04:22.639 Here's the prompt: we're on line one inside Pry with the main object. Like IRB, I can type valid Ruby code, such as 'require base64'. When I hit enter, Pry syntax highlights it, making it easier to follow.
00:04:54.960 What can be less obvious is how to use the Base64 library, but that’s where Pry's commands become valuable. I can run the 'ls' command, which behaves like a method but isn't.
00:05:10.720 The 'ls' command will display the list of methods available on a module, similar to how the 'ls' command in Bash lists files in a directory.
00:05:28.960 By running this, I discover there are various ways of utilizing Base64 in Ruby—indicating decode and encode versions, as well as strict and URL-safe options. Looking at this list helps, but sometimes I forget to append 64 at the end, which complicates things. My goal is to just experiment and learn more about the library.
00:05:51.680 So let's try playing with the library and see how it functions. I start typing 'Base64.decode64', but since I'm a poor typist, I pause midway and utilize type completion.
00:06:12.320 I’m sure you've encountered tab completion in Bash; it's even better in Pry. All this does is complete the method name for me. I execute 'Base64.encode64' and receive something resembling 'Base64.quote(