Dig Deeper with Pry: Explore Ruby Internals
TL;DR — you can now view the source for C identifiers in Pry
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:
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:
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.
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:
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):
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.
- 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