Creating a Ruby gem using Thor generator

I have been writing ruby gems for sometime now and always wish that there could be a command that can generate the gem structure/boilerplate and all the things I love pre-configured for me.

This is where Thor comes in handy. I am going to introduce you to Thor a.k.a the badass command line generator that will make your life easy when it comes to ruby gem creation.

I do want to mention Mariella Miranda who’s article on this topic inspired me to create a project which can be used as a gem generator to save you a lot of time.

just imagine this command:

Produces this:

Let’s do this!

Install thor

Clone gem generator repository

Link to my repo https://github.com/saluminati/gem_generator

Getting started

Your gem with all the required files is now created in the age_calculator directory

You can also append gem generator with use_rspec_suit and use_rubocop options to have rspec and rubocop pre-installed and configured with your gem

Example:

Once your gem is created, make sure that you try to build it.

Example:

Some explanation – As I promised

Content of gem_generator.thor file

Our main file is gem_generator.thor which holds everything together.

This file is extended from Thor::Group which, in a nutshell, execute all the methods defined in this class in the order they were defined.

If you look at the methods inside this class, they are creating the gem directory and copying the templates files within your gem namespace with everything per-configured.

Happy coding! please let me know if you run into any issue.

Written by

Leave a Reply

Your email address will not be published. Required fields are marked *