
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?

π¨ 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!
