DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

🚀 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
Enter fullscreen mode Exit fullscreen mode

💡 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.
Enter fullscreen mode Exit fullscreen mode
  • Creating the Gem Folder :
make_gem_folder creates a new directory with the name specified by the user.
Enter fullscreen mode Exit fullscreen mode
  • Creating the Gem Specification File (.gemspec):
crear_gemspec generates a .gemspec file using a template and places it inside the gem directory.
Enter fullscreen mode Exit fullscreen mode
  • Creating the lib Folder:
create_lib_folder creates the standard lib folder for the gem structure.
Enter fullscreen mode Exit fullscreen mode
  • Creating the Main Ruby File (lib/name.rb):
crear_main_rb creates the main Ruby file inside the lib folder.
Enter fullscreen mode Exit fullscreen mode
  • Git Initialization :
init_git_repo initializes a Git repository inside the gem directory.
Enter fullscreen mode Exit fullscreen mode
  • Adding a License :
copy_license allows the user to add a GNU license to the gem.
Enter fullscreen mode Exit fullscreen mode

⚡ How to Use the Generator Script

  • Make the Script Executable : First, ensure the script is executable:
chmod +x generator
Enter fullscreen mode Exit fullscreen mode
  • Run the Script : To create a new gem, run:
./generator my_gem
Enter fullscreen mode Exit fullscreen mode
  • 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/
Enter fullscreen mode Exit fullscreen mode

🌟 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!

Top comments (0)