loading...

Why I made a Gem for my Capstone

jhiatt profile image jhiatt ・4 min read

My Ruby on Rails capstone project for my bootcamp is a budget app with a calendar component. I decided to keep some info in a monthly date format and other info in a weekly date format (more on the pros and cons of that decision in my next article). Basically I had two sets of incompatible data with no easy way to integrate them, which I needed to do any of the math in my budget. This was really annoying.
Then one day, toward the end of the bootcamp we had a class on how to make your own gem. We used plenty of gems but I had no idea how easy it was to make one. All you do is write some code in your terminal and then code it up in Ruby.
In reality the reason I decided to make a gem for my capstone was because I thought it would be awesome! (I’ve been using these things for weeks and now I can make one!) But I wanted to share some insights I’ve gained from the process.

  1. Want to understand how languages work? Make a gem!
  2. When should you make a gem for your own code?
  3. Why you should just write code and not make a gem (most of the time).

1. Want to understand how languages work?

I remember being surprised to learn that Rails itself is a Ruby gem. While I admit I don’t know anything about how languages like Ruby are made, I assume the process is a little like creating a gem. You start with 1’s and 0’s and you write them in such a way that you can create a foundational language. Then you use that language to create a bunch of shortcuts to make it easier to code and the result is an entirely new language. The process continues until you get to Ruby. Ruby was then coded in such a way to create Rails, which makes Ruby much easier to use, the same way that Angular, React and Vue.js are written in JavaScript to make JavaScript easier to use.
In other words, creating a gem is a way to be a part of the process. Though I am brand new at this, I’ve always assumed that understanding how these languages work and where they’ve come from would be a pretty great way to deepen yourself as a programmer and prepare you for learning new languages.

2. When should you make a gem for your own code?

Fundamentally, a gem is a completely removed from the app you are working on. It is basically another app that is really easy to link to your current app. This means that it is more difficult to update your code, which can be a pain. On the other hand when it is more difficult to update your code, you can force yourself to leave the code alone which can be just what you need.
In my case, I had one specific problem that was actually pretty simple; I needed to convert from a year-date format to a week-date format and vice-versa. Aside from any bug fixing I knew I wouldn’t need to change that functionality and it would be handy to code it up and set it aside so I didn’t have to worry about it. After working on it for a little bit I realized it is also really easy to set up RSpec tests and run your own tests to test the functionality of the gem. If you find yourself with this kind of a problem, there isn’t a solution already out there you are happy with, and it’s a problem you might have in the future with a different app, it might be worth considering making a gem.
Of course there are plenty of downsides. The main one I found was that after using it for a while, I realized that my documentation for how to use the gem wasn’t complete and I still haven’t updated it as thoroughly as I should. There is a little more upkeep. It’s not difficult, just a hassle to remember.

3. Why you should just write code and not make a gem (most of the time).

This brings me to my last point. In Rails there are so many ways you can solve a problem. For simple ones like mine I could have easily made a model method that would have done the same thing. In reality the only time you 100% should make a gem is when the code is too complicated to hold in an app or too useful to hold in one app. For my personal preferences, making a gem made it easy to stay organized and focus on my other code (which needed some help). I also don’t know the impact of using a gem on things like computation speed and app size.
Please feel free to make any comments, correct any errors, or offer better explanations. I am happy to receive any feedback. I’m brand new at this and would love to get better. For those coders out there who have always dreamed of making a gem, but have been held back by naysayers: Do it! Live your dreams! I’m proud of you!

Discussion

pic
Editor guide