πŸš€ Automating Ruby Gem Creation with Thor: A Guide to the Generator Script πŸš€

March 28, 2025

Creating a Ruby gem from scratch often involves repetitive tasks such as setting up the gemspec, creating directories, and initializing version control. To simplify this process, I’ve written a Thor script that automates gem creation. In this post, I’ll walk you through the code behind the generator and explain how to use it to quickly generate a new gem structure.


Need Expert Ruby on Rails Developers to Elevate Your Project?

Fill out our form! >>

Need Expert Ruby on Rails Developers to Elevate Your Project?

πŸ”¨ The Script Overview

The provided script uses Thor, a Ruby toolkit for building command-line interfaces, to automate the creation of a Ruby gem. This generator script simplifies the process of creating the gem structure by organizing tasks such as file creation and running commands.

πŸ” Code Walkthrough

Let’s break down the key parts of the script:

#!/usr/bin/env ruby

require "thor"

class Generator < Thor::Group
  include Thor::Actions

  argument :name

  def self.source_root
    File.dirname(__FILE__)
  end

  def make_gem_folder
    empty_directory(name)
  end

  def crear_gemspec
    template("templates/template.gemspec.tt", "#{name}/#{name}.gemspec")
  end

  def create_lib_folder
    empty_directory("#{name}/lib")
  end

  def crear_main_rb
    template("templates/main.rb.tt", "#{name}/lib/#{name}.rb")
  end

  def init_git_repo
    inside(name) do
      run "git init"
    end
  end

  def copy_license
    if yes?("Use GNU 3 License? (yes/no) ")
      copy_file "templates/GNULICENSE", "#{name}/LICENSE"
    end
  end
end

Generator.start

πŸ’‘ How it Works:

  • Class and Initialization:
The Generator class inherits from Thor::Group to manage multiple tasks in sequence. The gem name is passed as an argument.
  • Creating the Gem Folder:
make_gem_folder creates a new directory with the name specified by the user.
  • Creating the Gem Specification File (.gemspec):
crear_gemspec generates a .gemspec file using a template and places it inside the gem directory.
  • Creating the lib Folder:
create_lib_folder creates the standard lib folder for the gem structure.
  • Creating the Main Ruby File (lib/name.rb):
crear_main_rb creates the main Ruby file inside the lib folder.
  • Git Initialization:
init_git_repo initializes a Git repository inside the gem directory.
  • Adding a License:
copy_license allows the user to add a GNU license to the gem.

⚑ How to Use the Generator Script

  • Make the Script Executable: First, ensure the script is executable:
chmod +x generator
  • Run the Script: To create a new gem, run:
./generator my_gem
  • Result: After running the script, you’ll get a gem structure like this:
my_gem/
  β”œβ”€β”€ my_gem.gemspec
  β”œβ”€β”€ lib/
  β”‚   └── my_gem.rb
  β”œβ”€β”€ LICENSE (if selected)
  β”œβ”€β”€ .git/

🌟 Conclusion

This Thor script automates the creation of a basic Ruby gem with the essential files and folder structure. It saves time and eliminates the need for repetitive tasks when starting a new gem project. With just a simple command, you can focus more on coding rather than on setup.


πŸ”— Get Started

You can find the complete code for this generator script on GitHub. Clone it and start generating your own Ruby gems with ease!

πŸ’¬ Have you used Thor to automate Ruby tasks? How do you streamline your development processes? Let me know in the comments!

Leave a comment