Dig Deeper with Pry: Explore Ruby Internals

John Mair
John Mair
Feb 15, 2018 · 3 min read

TL;DR — you can now view the source for C identifiers in Pry

Background

Pry has always been about digging through code — whether you’re debugging or trying to understand a code base. One of the more powerful commands for achieving this is show-source:

Image for post

Originally the show-source implementation only supported pure Ruby source viewing. This was a good start, but caused some frustration when users attempted to view the source for a C-backed method, such as Signal.trap.

So, a year or so after Pry’s release, the pry-doc plugin pushed further to support the extraction of source for Ruby methods written in C:

Image for post

This allowed much deeper exploration of code, but it still had its limitations. For example, the C identifiers trap_signm and RSTRING_PTR are out of reach as they do not map to methods in Ruby.

CRuby Support

These limitations always annoyed me as it necessitated leaving Pry and resorting to an external tool. And so I’m happy to announce the newest version of pry-doc supports source viewing for a large subset of CRuby C identifiers:

Image for post

This allows us to go beyond what is exposed to Ruby, and delve into the functions, structs, enums and macros that comprise the CRuby C code base. Also, since the same command is used to view both Ruby-land and CRuby source, we can seamlessly move between the two (note that $ is an alias for show-source):

Image for post

How to use it

To make use of this new feature simply install the latest version of pry-doc. You will also need to have curl installed as well as etags (this program comes standard on most Linux and macOS systems).

Upon first trying to view a CRuby identifier you will be prompted to download and install the relevant Ruby source. The source will then be installed in the ~/.pry.d/ folder and processed by pry-doc. After this you’re ready to go.

Limitations

  • Only CRuby identifiers can be looked up. Other C identifiers such as strcmp come from libc and we cannot yet retrieve their source (though this may be possible one day).
  • This is a first release so it may have some issues. In particular some of the data returned from etags can be quirky and this may result in strange behaviour.
  • Does not work on Windows, but I can add support if there is demand.
  • No tab completion (yet) for CRuby identifiers.
  • Only supports full-release Ruby versions ≥ 2.0.

File an issue @ https://github.com/pry/pry-doc if you encounter any problems!

You can also see my other projects @ https://github.com/banister

Enjoy!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store