Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Ruby gem executable

I can use gems like RSpec or Rails or Pry by calling their respective gem names, e.g. rspec, rails, pry on the commandline. How can I achieve this with gems I create? I'm using bundler for the basic gem creation.

like image 580
Kori John Roys Avatar asked Oct 20 '12 15:10

Kori John Roys


2 Answers

To make your gem executable in CLI, you should set the followings up.

  1. Place your executable file the bin folder, like bin/hello
  2. Make that executable by set permissions (chmod u+x bin/hello)
  3. Set up gemspec configuration accordingly (hello.gemspec)
spec.files  = `git ls-files -Z`.split("\x0")
spec.bindir = 'bin'
spec.executables << 'hello'

spec.executables considers bin as default folder for binaries and executables, though you can change it.

You can find documentation about this here: Gemspec#executables.

like image 165
Mark Avatar answered Sep 24 '22 21:09

Mark


According to documentation of Gemspec file you must put your executable in bin/ folder.

like image 22
Hauleth Avatar answered Sep 25 '22 21:09

Hauleth