Skip to content

Instantly share code, notes, and snippets.

@ahcode0919
Last active August 15, 2019 03:42
Show Gist options
  • Save ahcode0919/9cd476c8ce7bccb5eafae679f5754267 to your computer and use it in GitHub Desktop.
Save ahcode0919/9cd476c8ce7bccb5eafae679f5754267 to your computer and use it in GitHub Desktop.
How-to Create a Ruby CLI Gem

How-to Create a Ruby CLI Gem

Pre-Setup

  1. Create a RubyGems Account
  2. Install RVM (Ruby Version Manager) : RVM
  3. Install Bundler: gem install bundler

Project Creation

  1. Navigate to the parent directory you want to create your gem repository directory in.
  2. Create Gemset: rvm gemset create yourGemName
  3. Use Gemset: rvm gemset use yourGemName
  4. Bundle Gem: bundle gem yourGemName
  5. Open the app's parent file located at lib/yourGemName.rb
  6. Create a Hello World output
require "yourGemName/version"

module yourGemName
  class Test
    def say_hello
      puts "Hello World!"
    end
  end
end
  1. Create an executable file at bin/yourGemName (no file extension)
  2. Add Shebang #!/usr/bin/env ruby and code to call your Hello World output
#!/usr/bin/env ruby

require 'yourGemName'

test = yourGemName::Test.new
test.say_hello
  1. Create a CHANGELOG.md file in the project directory
  2. Create repository on Github
  3. Make initial commit: git commit -m "Initial commit"
  4. Push code to repository on Github:
git remote add origin [email protected]:yourusername/yourGemRepo.git
git push -u origin master
  1. Open gemspec - /yourGemName.gemspec
  2. Update todo lines:
    1. Update spec.summary
    2. Update spec.descriptions
    3. Update spec.homepage
    4. Update spec.metadata["source_code_uri"] (Github Repo Page)
    5. Update spec.metadata["changelog_uri"] (Github CHANGELOG.md link)
    6. Update spec.bindir to bin
    7. Update spec.executeables to yourGemName (the file in /bin)
  3. Run Bundle Install: bundle install
  4. Build Binary: rake install
  5. Test output: yourGemName --> "Hello World"

Deploying Gem

  1. Commit Changes to Github
  2. Build Gem: gem build yourGemName
  3. Push Gem to RubyGems.org: gem push yourGemName-0.1.0.gem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment