RubyMine 2024.1 Help

Create and publish your first Ruby gem

This step-by-step tutorial will show you how to create a simple Ruby gem and publish it to RubyGems.org.

Before starting this tutorial, do the following:

Create a Gem application

To create a new Gem application, follow the steps below:

  1. Run RubyMine and click New Project on the Welcome Screen.

    Welcome screen
  2. In the New Project dialog, select Gem on the left pane and specify the following settings:

    New Project dialog / Gem
    • Location: Specify a project's location and name (hello_rubymine in our case).

    • Ruby SDK: Select a required Ruby interpreter.

    After you’ve specified all the options, click Create in the New Project dialog. RubyMine will create a new Gem application.

Add code

Code for gems is placed within the lib directory. Our newly created project contains the hello_rubymine.rb file in this folder. Let's add working code for our gem:

  1. To open hello_rubymine.rb, press Ctrl+Shift+N, start typing hello_rubymine.rb, select this file, and press Enter.

  2. In the opened lib/hello_rubymine.rb file, add the following code:

    require "hello_rubymine/version" module HelloRubymine def self.greet(name) puts "Hello, #{name}! I'm Ruby!" end end

Provide gem specification

Every gem project has the *.gemspec file containing information for a gem. In our project, this information is stored in the hello_rubymine.gemspec file. Perform the following steps to provide the required data:

  1. Press Ctrl+Shift+N, start typing hello_rubymine.gemspec, select the hello_rubymine.gemspec file, and press Enter

  2. In the opened hello_rubymine.gemspec file, specify the required gemspec attributes.

    Gemspec file

    For our sample gem, we've specified the following fields:

    • authors: A gem's author(s).

    • email: An email address.

    • summary: A short gem's description

    • homepage: The URL of the gem's home page. We'll specify this address later after publishing the gem's code to GitHub.

    • metadata["source_code_uri"]: The gem's source code URI. We'll use the same value as for the homepage.

    • metadata["allowed_push_host"]: We removed this string from *.gemspec for publishing to RubyGems.

Share gem on GitHub

In this part, we'll publish our gem's source code to GitHub:

  1. Go to Git | Share Project on GitHub.

  2. In the invoked dialog, specify the name of the repository to be created on GitHub. Then, select the required account in the Share by field and click the Share button.

    Share project on GitHub dialog
  3. In the next dialog, you will be prompted to choose files for the initial commit. Leave the default set of files, specify the commit message, and click Add. Then, wait until a project is pushed to GitHub.

  4. After the project is created on GitHub, provide the homepage and metadata["source_code_uri"] attributes in the project's *.gemspec file (https://github.com/rubyminedoc/hello_rubymine in our case). Then, commit and push changes made in *.gemspec.

Build gem

After we've made all changes in *.gemspec, we can build a gem:

  1. In the main menu, go to Tools | Gem | Build Gem.

  2. In the invoked dialog, click the Build button to build the gem. RubyMine will display the information about the created gem in the Run tool window.

    Build gem

    You can see the created gem in the Project view (Alt+1).

Install gem

After building a gem, we can install it to a local SDK for testing:

  • Press Ctrl twice and enter the following command:

    gem install hello_rubymine

    Press Enter to install a gem.

Test gem with IRB

In this chapter, we'll test our gem using the IRB console by calling it's greet method:

  1. In the main menu, go to Tools | Run IRB Console.

  2. Type require 'hello_rubymine' and press Enter to load our gem to IRB.

  3. Then, type HelloRubymine.greet("JetBrains") in a console and press Enter again to make sure our gem works as expected.

    Test gem in IRB console

Publish gem

Finally, we'll publish our gem to RubyGems.org. Note that you need an account at RubyGems.org to do this.

  1. Go to Tools | Gem | Push Gem.

  2. In the Run tool window, specify your RubyGems credentials.

    Push gem to RubyGems

    Your gem will be published to RubyGems.org.

Last modified: 08 April 2024