Summarized using AI

Pry Until You Die

Tay DeHerrera Jimenez • October 07, 2024 • Boulder, CO

In the presentation titled 'Pry Until You Die' by Tay DeHerrera Jimenez at the Rocky Mountain Ruby 2024 conference, the speaker delves into the powerful Ruby developer console tool known as Pry. The focus of the talk encompasses understanding Pry's mechanics, its practical applications, and how it enhances productivity for developers.

Key points discussed include:

  • Introduction to Pry: Pry is presented as a runtime developer console and an IRB alternative that offers advanced introspection features for Ruby programming.
  • Getting Started with Pry: The speaker outlines the basic installation and functionality of Pry, including essential commands like LS, CD, and EXIT, emphasizing its interactive REPL (Read-Eval-Print Loop) capabilities.
  • Pry Plugins: Key plugins such as 'pry-doc' and 'pry-rails' are featured. Pry-doc facilitates viewing documentation directly in the Pry session, while pry-rails enhances the Rails console experience, allowing for easy access to model attributes and routes.
  • The Functionality of binding.pry: One of the most powerful features, binding.pry, is explained. The speaker describes how this command halts execution to allow developers to interact with the code's current state by taking a snapshot of the execution context.
  • Process Management: The discussion extends to the challenges of integrating Pry with process management tools like Foreman and Overmind, highlighting how these tools can complicate the debugging experience when multiple processes run concurrently.
  • Final Thoughts: The speaker advocates for Overmind over Foreman for its superior control when managing processes simultaneously, supplemented by anecdotes from personal experiences.

Overall, the presentation not only illustrates Pry as a critical tool for Ruby developers but also shares strategies for maximizing its effectiveness, ensuring participants leave with practical insights to apply in their own development workflows.

Pry Until You Die
Tay DeHerrera Jimenez • October 07, 2024 • Boulder, CO

In this talk, I will dive into the ins and outs of Pry, the powerful runtime developer console for Ruby. Many developers know about Pry, but do they understand how it works under the hood? How does `binding.pry` halt code execution and inject itself into the process?

We'll explore the mechanics of Pry, revealing how it integrates seamlessly with your code. Additionally, I'll share essential tips and tricks to maximize your productivity within Pry sessions, including:

- Customizing your Pry environment
- Navigating and debugging code efficiently
- Utilizing advanced Pry features and plugins

Whether you're a Pry novice or a seasoned user, this session will enhance your debugging and development skills, enabling you to harness Pry's full potential.

Rocky Mountain Ruby 2024

00:00:13.559 good morning everybody good morning yes I am very excited and a little bit
00:00:19.920 nervous to be here with y'all today and I am going to be talking to you about a tool that I really really really love
00:00:28.039 called pry um yes right yes um so I just wanted to
00:00:35.719 give a little bit of intro about who I am little bit of background it would be
00:00:43.559 completely unlike me if I didn't mention that I was from PUO Colorado and I am so proud to be from
00:00:49.480 there shout out 719 um I am a CU Boulder grad I
00:00:55.680 graduated with my degree in math in 2015 and then I know we were talking a
00:01:02.120 little bit about Turing earlier and how they used to be involved with Rocky Mountain Ruby but I am a turing Alum I
00:01:08.200 graduated yes okay um I finished in 2019 and for
00:01:15.479 those of you that do not know Turing was a coding boot camp in Denver Colorado um
00:01:21.720 so yeah that's a little bit about me I am a pride guy I'm your pride guide for today and honestly I have to take it
00:01:31.360 when I was thinking about this talk and all the places like where I could start
00:01:36.720 I really was just thinking that I've been using price since like the very very
00:01:42.159 beginning and maybe I was indoctrinated at Turing but I really like have never
00:01:47.840 really touched IRB or bubug um I don't really know them like that
00:01:53.840 but when I was studying at touring I was studying back in development and you
00:02:01.280 know I see a few familiar faces here who know what it was like to be like in that basement working 60h hour work weeks
00:02:07.280 like no job tensions were very very high a lot of the time um but I had a friend
00:02:15.959 there I met a friend her name was Stella and we spent so many late nights in that
00:02:21.319 basement just trying to put two and two together and make sure we can make sense of it all and whenever we couldn't do
00:02:28.400 that we would always tell each other like baby you have to Cy until you die and just like figure it out you just
00:02:35.280 have to figure it out um so yeah I've really been using price since the beginning of my career and I've been
00:02:41.959 doing this like a little bit over five years um
00:02:47.640 so yeah I wanted to give just a brief little introduction about what pry is
00:02:52.840 first of all I know that a lot of people here have a lot of different familiarity
00:03:01.120 um with software development I'm not sure how everybody else like got into it um but I'm sure we all use and know
00:03:08.319 about pry but just taking it straight from their GitHub pry is a runtime developer console and I just spilled all
00:03:15.920 my water this is literally my worst nightore um so PRI is a runtime developer console
00:03:23.720 and IRB alternative with powerful introspection capabilities pry aims to be more than an IRB replacement it is an
00:03:30.560 attempt to bring reppel driven programming to the Ruby language so for me that was kind of a lot of words like
00:03:37.319 a lot of big words in there particularly um the runtime developer console bit and
00:03:46.680 when I was researching this I had to kind of like thank you so much um I had
00:03:51.840 to kind of research a little bit about like what those words actually meant so just breaking it down runtime you were
00:03:58.760 able to insert into a live application stopping the execution at any point and
00:04:04.640 you can interact with the current state of the program and then as for developer
00:04:10.079 console what we mean by that is just an interactive environment where we can run code inspect variables and navigate
00:04:16.359 through the code base and kind of tangentially to that um I know we've
00:04:24.759 heard the word repple before but I don't know if everybody knows exactly what that means but reppel stands for for
00:04:30.479 read evaluate print and loop so pry is a
00:04:35.800 repple and that is just an interactive programming environment that can take
00:04:41.639 user input which is the r the read evaluate that input which is the E they
00:04:47.680 can print it to the terminal and then Loop meaning that we wait for the next
00:04:53.840 command so that is just kind of like a little bit of a background as to what pry
00:05:00.160 actually is now we can talk about getting started
00:05:05.600 with pry um thank you so
00:05:13.039 much
00:05:24.720 pry there is really
00:05:29.840 kind of three main gems that I use when I use pry and it's something that I kind
00:05:36.199 of didn't realize that I use like Al together until I started doing the research for this presentation so
00:05:42.400 generally when you just do your gem install pry what that does is it gives you kind of like an IRB situation like a
00:05:49.160 reppel type thing that we were talking about in the previous slide and you know
00:05:54.759 there's not a lot like noteworthy there um and
00:06:00.080 you have like all of the simple commands there that you all know about maybe which is like LS to list all of the variable methods CD to navigate objects
00:06:07.800 and exit to actually leave through the pry and what I want to do right
00:06:14.199 now is just like a quick little live demonstration so this is just like a
00:06:19.440 small little app that I built over the weekend to demonstrate this but you can
00:06:24.880 see here in my gem file here I have gem pry pry
00:06:34.400 do and pry rails so now that we know I have those installed I should be able just to
00:06:42.319 type pry on the command line and then this will enter us into an IRB situation
00:06:47.680 so just to give you a brief example on what that looks like like 2 plus 2
00:06:53.720 equals 4 so we were able to input 2 plus two it was able to evaluate it and print it to the con
00:07:00.080 and then we're ready to go again if we need to do something else so not too much um to note there but kind of
00:07:07.039 something cool to know I guess like you could say x = 2 y = 3 and then oh sorry
00:07:16.080 but do it right um now just to show you like that
00:07:22.160 LS command you can see X and Y highlighted in yellow over here and that
00:07:27.520 was actually something that I didn't really know of until recently so any variables that you set in your price
00:07:33.759 session if you type in LS it'll show all of what you have access to right on the
00:07:40.199 terminal which is helpful for me because sometimes I find myself like in these long priz sessions like all day and it's
00:07:48.440 just helpful to like know all of the the variables that I've like collected over like all of these hours of work or
00:07:54.199 whatever maybe that's like not the best thing to do but it's what I do um so
00:07:59.280 that just to demonstrate PRI like at a very very basic Baseline level just as
00:08:04.879 like a repple so now what I want to talk to you all
00:08:12.440 about is oh yeah back here that was getting started with pry now I am really
00:08:19.960 really excited to talk to you all about pry do so this is one that you might be using that you might not be aware that
00:08:26.319 you're using but it is a plugin that enhances the documentation capabilities of the pry
00:08:32.360 console it allows you to view documentation for Ruby classes modules and methods directly within the price
00:08:38.320 session so what I mean by
00:08:44.640 that is that if I come back here let me
00:08:51.279 actually ra see um let me think here
00:09:03.959 amazing Okay cool so what Pride do does
00:09:09.200 is it gives you the ability to you know get the documentation straight from your
00:09:16.480 terminal for Ruby source code or really like anything that you have access to like within your app and this was such a
00:09:23.320 cool feature that I did not know like existed but I've been using it like for the last couple months and it's been
00:09:28.600 like a major game changer in my workflow and what I'm bringing to work um so yeah
00:09:34.920 this is just like the array class and it brings up like all of the the documentation as I said um if you're the
00:09:41.880 kind of person that likes to have line numbers next to your documentation it also supports that with the- L option
00:09:50.279 and there's one other cool little trick if you give it Das D oh maybe we won't see that
00:09:58.200 there um watch I'll
00:10:09.839 dasd flag so right there you just see like the actual source code and it actually tells you like where it's
00:10:16.519 coming from who the owner is the method signature like what that looks like which is very valuable um there's all
00:10:23.399 the source code if you care what like that looks like but if I add that d d flag then it tells you a little bit like
00:10:30.240 how to work with this particular method so you can do that literally like on any
00:10:35.519 kind of Ruby rails method um so yeah
00:10:40.680 that's one thing that I found was really helpful in my
00:10:47.600 research and then yeah the next thing that I wanted to
00:10:55.240 talk to you all about is something that I actually just demonstrated but is like very subtle
00:11:00.880 um and with that I want to talk a little bit about pry rails um again this is
00:11:08.800 another one that you all probably use or have heard of and maybe you aren't aware that you are but this plugin is one that
00:11:15.800 integrates with pry with rails applications enhancing the rails console experience by replacing the default IRB
00:11:22.839 shell with pry so yeah what that means is that if I
00:11:29.920 had not had installed pry rails into my application if I were to go to rail C it
00:11:37.600 would by default open up IRB because that is the default that rails ships with but now since I have installed this
00:11:45.079 gem pry rails when I do rail c will automatically spun up into a console and
00:11:51.920 you can note that here so it says pre which I think is pretty neat um but when
00:11:58.680 I was is researching this um I was kind of like under the
00:12:05.360 assumption that this is like the only thing that uh uh pry rails
00:12:13.240 does um is that it sets the interactive console as pry but it actually does a
00:12:20.040 little bit more than that um it supports a couple other methods which I'll show you now
00:12:26.320 so if I do actually help cool so if you type help into the
00:12:31.720 terminal it'll actually tell you all of the methods that you have available to it but two in particular that are
00:12:39.440 helpful for me actually don't see
00:12:44.920 that yes okay cool so if you find this little
00:12:49.959 sorry um if you find this little section it says rails then you get all of these
00:12:55.399 methods with the pry rails Gem and one of them in particular that I
00:13:03.360 use all the time is this show model method as well as show routes um and
00:13:08.760 just to kind of give you a visual of that um
00:13:15.079 so show model so I built like a little diary app
00:13:21.959 and as of right now like what I have in there is just a user a journal and entries and I mean for smaller classes
00:13:28.959 it's it's like or for smaller apps it's like easy to navigate back to your text editor and just kind of like find the
00:13:35.040 appropriate files or the model classes um but you know as soon as like we start
00:13:40.639 getting like larger applications with like a lot of different models on there it's kind of hard and cumbersome to
00:13:46.320 remember like the attributes on whichever model you're dealing with or the associations that go along with it
00:13:53.279 so something that's like I'll do that so you all can see is helpful is that if if
00:13:59.199 you do show model and then you know pop in the model name it'll tell you all of
00:14:04.560 the attributes and all of like the associations that you have with it but then kind of taking that a step further
00:14:11.120 and putting like all three of these things together like you could also oh I
00:14:16.680 don't think it'll actually work uh show Source user yeah okay so my
00:14:25.399 user class right now is like pretty empty aside from like it's validation and it associations but if I were to put
00:14:32.800 like an instance or a class method like in there then if I were to do show Source in user then it would show me
00:14:38.800 like the entire file that I'm able to um or that I have like written in my text
00:14:44.000 editor so there's like no need to kind of like bounce back and forth and then like I said there is
00:14:50.959 also um some other cool
00:14:56.040 methods show routes is another one that I use a lot so if you want to do show
00:15:03.000 routes from within the console it'll tell you
00:15:08.040 like it's so ugly it'll tell you like all of the routes that you have like in
00:15:13.880 your application but sometimes it's not so helpful because you have so many routes but what is great is that it
00:15:21.279 supports like the GP functionality so I can if I just care about like entries
00:15:29.360 one second
00:15:40.480 wow did that do it yeah okay yeah okay well it does do that it does
00:15:48.519 the I've seen it work before with my own eyes um cool so like those are my three
00:15:54.839 main players like when I am debugging with prry so I use like the straight up pry Gem and then pry docks and pry rails
00:16:04.160 um so those things are really cool but okay to be
00:16:11.319 honest I think I use the rails console with pry pretty often like very often
00:16:19.959 but I don't know about you all but when I am debugging locally it is like
00:16:29.160 it is more often than not I using pry um by doing like a binding dot pry
00:16:36.319 and that is probably like the most powerful feature that pry has to offer me but when I was kind of thinking about
00:16:42.800 how I wanted to talk about binding. pry I was thinking like how exactly does
00:16:48.040 that work like what do the like what is binding and what is like this pry method that like goes onto it
00:16:55.240 so yeah that was the slide pretty much um so how does it work how is the
00:17:02.639 execution of the code halted well for that there is kind of a combination of
00:17:10.000 things at play it's a combo of control flow interuption so basically when the
00:17:16.240 Ruby interpreter comes up and reads the binding.pry line or the line that has
00:17:21.640 the binding. pry then the Ruby interpreter knows to just Hal execution
00:17:26.720 stop and then you're able to do it ever you want with your pry um and then after that we have thread
00:17:35.320 and Process Management so if you've ever noticed when doing binding.pry you will
00:17:41.240 notice um that the execution context is suspended it's not terminated so you're
00:17:47.320 just kind of like hanging there and you're able to interact with the current context so those are kind of like subtle
00:17:54.320 things on how it works like high level things I guess um but but if we want to
00:18:00.559 dig a Little Deeper we can so Bing object so binding not pry
00:18:08.120 binding is actually an object which I didn't know like it makes sense but in
00:18:13.200 my head it makes sense but um one of those things I didn't know about so what
00:18:19.360 is cool is we can kind of put it all together so we can do show Source
00:18:25.760 on do this actually binding so then show Source B and then let's
00:18:35.159 actually get the line numbers and then the r
00:18:41.159 do oh maybe not the r do okay
00:18:50.000 so this is kind of cool this is kind of this is the source
00:18:56.159 code for The Binding object so when you call binding.pry this is kind of like the first part of that so you can see
00:19:02.240 here that it instructs the web console to render a console in the current binding without the need to enroll the
00:19:07.720 stack raises double render error if a more than one console invocation per request is detected um but what does
00:19:16.799 that mean what does that mean um okay so what that kind of means in
00:19:27.320 regular words is that uh The Binding object captures the current context you
00:19:32.799 can kind of think of it as like taking a photo of where you are are at in your application wherever you decided to use
00:19:39.240 your breakpoint and hold the execution of code so at that point when you call binding.pry the Ruby interpreter hals
00:19:46.600 the execution and then The Binding object takes like a snapshot of all of the local variables all of the instance
00:19:53.159 variables methods self um it takes a complete snapshot of that and cap that
00:19:59.200 current context so that is really cool that is kind of like what is setting us up to be
00:20:05.159 able to play around and like do really cool neat things um so what happens when we
00:20:14.960 call do pry on that so pry is called on The Binding
00:20:20.200 object that holds the current context this opens up an interactive price session where you can access variables
00:20:26.159 in scope execute Ruby code call methods step through code execution so all of
00:20:32.720 that good stuff but if you kind of want a little bit of a more detailed explanation you can use show Source here
00:20:40.799 and then oh wait let me do
00:20:47.159 this perfect amazing Okay cool so dashd worked on that uh that that command that
00:20:55.679 time um so yeah this is is what happens when you call the method pry on The
00:21:01.799 Binding object so uh start a pry repple on self if self
00:21:07.679 is a binding uh then that will be used to evaluate expressions otherwise a new binding will be created and then just
00:21:15.039 kind of goes into detail like how you can use it and then what is actually going on kind of behind the scenes um if
00:21:22.679 you note here on line 27 there is this py. start and a few arguments pass to it
00:21:29.960 py. start is like another way that you can just uh uh start an interactive
00:21:35.159 price session like if you're in IRB and you're not using pry rails then you can just do pry start from IRB and it'll pop
00:21:42.320 you into pry session which is pretty cool
00:21:47.880 um so nice that was kind
00:21:53.120 of pry how it works how I use it um but
00:21:58.760 something I wanted to also touch base on was pry and Process Management tools
00:22:08.679 um yeah I know most of
00:22:16.120 us um are probably working on like larger scale apps that require having
00:22:21.600 some sort of Process Management tool and what I mean by this is a tool that is
00:22:27.400 used to run multiple processes simultaneously in development so this is for apps needing
00:22:34.720 several components running concurrently so you can things think of things like your web server or like something to run
00:22:40.159 your background jobs or you know do whatever with your JavaScript redis webpack rails sidekick all of it um so
00:22:49.400 yeah we're typically working on apps that are dealing with these Process Management tools and one of the more
00:22:55.480 common ones is called foran so foran is a manager for Pro File
00:23:02.919 based application it aims its aim is to abstract away the details of the proc
00:23:09.360 file format and allow you to either run your application directly or export it to some other Process Management format
00:23:19.799 so that's pretty cool
00:23:25.120 um yeah so it's designed to manage these processes that are defined in a proc
00:23:31.720 file but this comes with some caveats when you are wanting to do some local
00:23:38.799 debugging or development and still wanting to use pry because how many times have you all like
00:23:46.440 ever put like a breakpoint in your code and then maybe you're using foran and
00:23:51.480 you go to spin up form and and try to pop yourself into your price session and
00:23:57.200 then as soon as you do as soon as you like start typing like all of the output from maybe a job that is running like
00:24:04.360 starts to display itself on your terminal screen and then all of a sudden the keystrokes that you're making like
00:24:11.480 aren't registering correctly and things are like broken and very scary and you can't do anything so that is like a I
00:24:20.240 would say like a huge deterrent from wanting to use prior being able to have that functionality usually what I do in
00:24:27.000 like these scenarios is is I will go to like my proc file Dev and in each terminal in my or each in separate
00:24:34.440 windows in my terminal I'll run each process um separately and then with
00:24:39.480 doing that I'm able to do like a rails s and I'm able to hit my binding.pry but that is a lot of work um very
00:24:47.480 cumbersome and it's actually something that I wanted to talk to you all about
00:24:53.559 here in this rails issue um
00:25:00.760 here there's a lot of conversation on it um there's a lot of conversation it was
00:25:06.279 written by dhh and there was a important for the release of rails 8 which was
00:25:12.760 last week so there's been a lot of um comments on this this issue um but
00:25:20.799 basically what he goes on to like outline here is kind of the problem that
00:25:26.720 I was just describing in the previous slide um basically you know you get like
00:25:34.440 that the job output rendering the terminal like not useful and then the keystrokes are all weird um we know why
00:25:42.240 this happens it's probably because of the underlying processes running due to Forin and
00:25:51.080 yeah sorry one second
00:26:00.640 Okay cool so this issue yeah describes the
00:26:05.799 scenario we were just discussing written by DH dhh and in his in this um issue he
00:26:13.720 kind of goes on to detail a solution and the solution involves creating a custom
00:26:20.440 bin Dev script that starts form in in the background while keeping the rail server as the main active process in the
00:26:27.960 termin and this would be helpful because then the debugger can still work directly as
00:26:35.240 rails runs in the foreground and I also just kind of want to note really quick that the issue that he makes here and
00:26:43.399 that he's describing is not um specified to Pride this also happens with debugger
00:26:50.520 as well and I think that's what he's talking about in um so yeah he goes on
00:26:56.559 to outline like a potential Sol solution but this comes with a a few drawbacks and one of those is that the web process
00:27:04.880 duplication um since the proc file typically defines a web process um but
00:27:10.520 we should we shouldn't we shouldn't be doing so because it already being it's already being handled by the rails
00:27:17.919 process um and one other thing that he goes on to outline in here is that they
00:27:23.919 need to fix is the fact that foreman usually outputs time steps which has a
00:27:30.279 tendency to clutter up the logs and maybe that that that can be disabled
00:27:37.200 um so yeah and then one other interesting point that he makes in here
00:27:42.480 is that this is important for rails 8 because rails 8 is shipping with solid Q
00:27:49.159 um and solid Q is going to be the default job process which will require a
00:27:54.640 background process to manage job cues so the solution that dhh goes on to
00:28:01.519 describe should work for solid Q as well as foran where the rail server runs as
00:28:08.039 the primary process and the solid Q process runs in the background um so
00:28:15.279 yeah I know that was like a lot of words but I just wanted to share like some funny like anecdotal stuff before I get
00:28:21.120 back to my presentation um there's somewhere in this um issue where
00:28:29.760 dhh goes on to explain how it's not a deal breaker for him
00:28:35.399 that the job output clutters up like the terminal so
00:28:41.120 long as the keystrokes work and everything renders as you expected but
00:28:46.159 for me that is would be like a huge deal breaker and something that would push me
00:28:51.640 away from using Forin um but something that is even more interesting is that somewhere else down here
00:28:59.559 someone goes into well why do we have to use foran what about overmind um it's
00:29:08.640 somewhere in this long conversation thread but I can talk to you a little bit about overmind because it ties into
00:29:14.320 what we were talking about with the process management tools so overmind is
00:29:20.440 a process manager for proc proc file based applications and utilizes t- with
00:29:26.919 overmind you can EAS run several processes from your proc file in a single terminal so just kind of touching
00:29:33.240 on that ex um overmind extends for in by using t-mo and allows managing processes in
00:29:40.720 separate t-mo panes for better control this is different than foran because
00:29:46.200 what overmind is suggesting is that in your terminal you will be able to kind of teas a pay or tease apart different
00:29:53.760 pains which relate to different processes um um whereas in foran you
00:30:00.080 only have the option to use one pain for all of your processes which makes it
00:30:05.159 kind of unusable um so yeah that is kind of why I prefer
00:30:11.600 overmind over Forman because with overmind if you want to go ahead and do your binding. pry you can do so as you
00:30:18.720 would with Forin but if you go back to your terminal and just type in overmind Connect then it will open a separate pan
00:30:26.159 with your binding. pry and you can use it as expected it's really nice um
00:30:31.720 something else that I thought was funny was like what is t-x I don't know what that means oh um and then I looked to
00:30:39.000 see what it means and it said it's a terminal multiplexer which sounds made up
00:30:45.640 um like I can't believe I'm here talking to yall about terminal multiplexors right now like that is so crazy um but
00:30:52.679 basically what that means is it lets you switch easily between several programs in one terminal detach them and reattach
00:30:59.559 them to different terminals so it's very valuable I suggest maybe taking a look
00:31:05.480 into it um so
00:31:10.840 yeah that's the end of my
00:31:19.639 presentation um something that I used that was pretty cool was this tool called
00:31:28.480 merp or something like that uh marp it's marp um but what basically what it did
00:31:34.200 was it took all my I typed all of my slides out in markdown and then it made
00:31:40.320 my it made all of my markdown into slides but basically why I wanted to bring that up is that maybe I can post
00:31:47.480 this something or post this somewhere for people if they're interested because it has like all of the documentation for
00:31:53.279 everything um if you all care so yeah let me know thank you
Explore all talks recorded at Rocky Mountain Ruby 2024
+22