Update 2021-05-02: Tutorial belajar membuat Ruby Gem dapat di situs resmi rubygems.org pada url https://guides.rubygems.org/make-your-own-gem/.

Artikel ini saya tujukan untuk pemula. Artikel ini bertujuan untuk mengetahui bagaimana cara membuat Ruby gem hello world. Sebelum membuat gem, install terlebih dahulu tool versioning yang bernama git. Kemudian buat akun di github dan gemcutter serta buat repositori bernama hello-world di github. Berikut ini adalah langkah-langkah yang dilakukan untuk membuat gem hello world.

Pertama kali yang dilakukan adalah install gem gemcutter dan jeweler. Perlu diketahui, versi jeweler yang digunakan saat membuat artikel ini adalah 1.4.0

gem install gemcutter
gem install jeweler

Jalankan perintah jeweler

jeweler hello-world

Masuk ke dalam direktori hello-world, kemudian buka file hello-world.rb di dalam direktori lib dan tulis kode seperti di bawah ini.

# hello-world/lib/hello-world.rb
puts "hello world!"

Buka file Rakefile dan isikan sebagai berikut.

#----------------cut---------------
  Jeweler::Tasks.new do |gem|
    gem.name = "hello-world"
    gem.summary = "one-line summary of your gem"
    gem.description = "longer description of your gem"
    gem.email = "youremail@example.com"
    gem.homepage = "http://github.com/kuntoaji/hello-world"
    gem.authors = ["firsname lastname"]
  end
#----------------cut---------------

Buat versi gem, build dan install gem

rake version:write
rake build
rake install # disarakan sebagai root saat menjalakan perintah ini

Langkah terakhir, berikut ini adalah kode untuk menggunakan gem hello-world.

require 'rubygems'
require 'hello-world'

Berikut ini adalah langkah-langkah untuk merilis gem yang telah kita buat ke publik dengan cara memasukkan ke dalam repositori gemcutter dan akun github.

git add .
git commit -am "this is hello world gem"
rake release

Sebagai informasi tambahan, jalakan perintah rake -T untuk mengetahui task apa saja yang tersedia untuk membangun sebuah gem.

aji@slacky:~/lab/hello-world$ rake -T
(in /home/aji/lab/hello-world)
rake build                           # Build gem
rake check_dependencies              # Check that runtime and development dependencies are installed
rake check_dependencies:development  # Check that development dependencies are installed
rake check_dependencies:runtime      # Check that runtime dependencies are installed
rake clobber_rdoc                    # Remove rdoc products
rake gemcutter:release               # Release gem to Gemcutter
rake gemspec                         # Generate and validates gemspec
rake gemspec:debug                   # Display the gemspec for debugging purposes
rake gemspec:generate                # Generates the gemspec, using version from VERSION
rake gemspec:validate                # Validates the gemspec
rake git:release                     # Tag a release in Git
rake github:release                  # Release Gem to GitHub
rake install                         # Install gem using sudo
rake rdoc                            # Build the rdoc HTML Files
rake release                         # Release gem
rake rerdoc                          # Force a rebuild of the RDOC files
rake test                            # Run tests
rake version                         # Displays the current version
rake version:bump:major              # Bump the gemspec by a major version.
rake version:bump:minor              # Bump the gemspec by a minor version.
rake version:bump:patch              # Bump the gemspec by a patch version.
rake version:write                   # Writes out an explicit version.

Update 2010

Saya menambahkan penjelasan bagaimana membuat gem hello-world executable sehingga bisa dieksekusi langsung dari command line. Berikut ini adalah langkah-langkahnya.

Buat direktori bin dan buat file hello-world dan tuliskan kode ruby di-dalamnya.

# bin/hello-world
puts "this is executable hello-world"

Jika di Linux, ubah permission.

chmod 755 bin/hello-world

Ubah Rakefile

Jeweler::Tasks.new do |gem|
  gem.name = "hello-world"
  gem.summary = "one-line summary of your gem"
  gem.description = "longer description of your gem"
  gem.email = "youremail@example.com"
  gem.homepage = "http://github.com/kuntoaji/hello-world"
  gem.authors = ["firsname lastname"]
  gem.executables = ["hello-world"] # ------------> add this line
end

Commit perubahan.

git add .
git commit -am "making executable hello-world"

Rilis gem.

rake gem:bump:major
rake release

Install.

gem install hello-world

Cara penggunaan.

aji@slacky:~$ hello-world
this is executable hello-world