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)