Debugging Someone Else's Rubygem

Published on Feb 07, 2015

We use a lot of other people's code... but what if their code has a bug? This screencast shows how to debug someone else's rubygem, and then make a pull request to their project.

Code

Transcript

Hey, and welcome to SparkCast number 15. I spend a lot of time in the Ruby on Rails and the Ember.js ecosystems. While it's great that there's so much code out there, it's not always perfect code. Sometimes you'll find a bug in someone else's Ember CLI add-on or their gem. Today we're going to be talking about the process of debugging someone else's Ruby gem and then committing that change back to the main branch.

For our demonstration today, we're going to be using the app we started developing last time in SparkCast 14. We're going to be editing the ember-cli-rails gem. Our first step is going to be to unpack the gem. What this means is that we're going to take the code and dump it out into a directory. We're going to specify the gem and then we're going to specify the target directory, which here will be "vendor/gems." You can see that in our vendor/gems folder, we now have the ember-cli-rails directory and in that there is all the code for the gem. We can look at it and modify it however we want. For our project to be able to use it, we need to specify that that's where we're getting it from so we will first do the version, which is required if you're doing this. Then, the path which will be "vendor/gems" and then the name of the folder. Then, you're going to want to make sure to run bundle install so that you're using your customized version of the gem.

At this point, you can start making the changes to the code that you have locally and then you can run your app and see if the gem works correctly. Typically, when you do this you'll be wanting to correct something that's actually causing your app a problem. In this case, for the case of the example, I'm just going to change a little bit of wording on one of the error messages.

You could stop here. However, this has the disadvantage of carrying all this code with your app instead of keeping it in a separate repository. It also doesn't propagate your changes to the wider community so no one else would get to benefit from your work. So what we're doing from here on is finding a way to get your work to the rest of the community.

The first step is to go to the original repository for the gem and then fork it. There, I've already forked it but if I hadn't this would have created a new one. Now that you have your fork, you grab the URL to clone it, and then you clone it into your own folder, you go into that folder, and then open it up in your text editor. Then you can find the code and make the same changes.

Here, you're going to want to check out a branch that describes what you're doing and then commit your changes, with a probably better explanation than I'm giving here, because you're going to want to convince the original maintainer that your changes will improve their project. Push this up, and then you can pull request to the original repository. You leave a comment here. Hopefully you can be as literate as I am here, they appreciate that a lot. Then, hopefully you'll get some good comments on your code and some suggestions on how to make it better, as well as acceptance into the original project so that everyone's code is now more awesome. When you're ready, hit Create Pull Request and there you go.

That is how you edit, debug, and make improvements to someone else's gem. I hope you enjoyed it, and happy coding!

Subscribe to our mailing list