You’re closer than you think to a career in tech. Our grads have launched rewarding new careers — you can too.View Our Jobs Report
Marcus Aurelius was a Roman Emperor from 161 to 180 AD. He is considered as the last of the five good emperors and is also known as one of the most important Stoic philosophers.
He wrote a book called ‘The Meditations’ which was a sort of diary for self improvement and guidance. The title was added after he died. He called this book as simply “To Myself”.
Almost two thousand years later his quotes still have much to teach people. In my case it was how to create my first ruby gem!
The Meditations is filled with deep and motivational quotes. My goal was to create a gem called the ‘meditations’ that would take in the book Marcus wrote and randomly return one of his quotes when called.
I downloaded the book from MIT classics and included it into the gem file.
I followed the online guide of how to create ruby gems. They are a lot easier to make than I thought it would be.
I created a class called Meditations which is the same as what the gem is called. The thing that took the most time on this little side project was to figure out how to include the correct filename. After struggling for a while Spencer and Jon helped me figure out the correct syntax for requiring gemfiles which is seen in the code below.
I then cleaned up the code from the book using several gsub methods and then split the book into quotes using regular expressions.
I then have two class methods. The one which is used most of the time is Meditations.quote which outputs a random quote from the book and the other is the Meditations.all method which gives all of the quotes.
To turn this into a gem we had first sign up for an account at rubygems.org. We then organise the structure of the file you want to upload so that the main directory has a meditations.gemspec file and a lib directory. Note that meditations is the name of the gem. A person creating a new gem would replace this with the name of their gem.
The .gemspec file contained the following code written in ruby.
The lib directory contains meditations.rb which is called the same thing as the gem and is run when the gem is called. This contained my code for the Meditation class.
Lastly, I created another directory in the lib folder called Meditations which had all of the extra files needed for the gem. In my case, this was just the actual book which I called ‘the_meditations_book.txt’. This is in lib/meditations/the_meditations_book.txt.
We then want to test if this works as a gem on our local machine. We run the folling commands.
We then test this in an irb terminal. We open irb with an extra handle so that is can locate the extra gem files.
we then require ‘meditations’ as you would any gem and test to see if it works.
The final step is to push it to RubyGems.org so that everyone else can download your amazing gem!
To do this we simply:
and give your credentials. If there were no problems other people will be able to simply gem install meditations and try it out!
I had a lot of fun building my first gem and I hope there will be many more to follow!
Blog Post Author
Since we opened our doors in 2012, thousands of students have joined Flatiron School to launch new careers in tech.
Find the perfect course for you across our in-person and online programs designed to power your career change.
Connect with students and staff at meetups, lectures, and demos – on campus and online.
Have a question about our programs? Our admissions team is here to help.