suranga
suranga

Reputation: 228

How to run a ruby program without specifying the path

I have built my program and installed the gem using

$sudo gem install ./helloworld-0.0.1.gem 

Now I need to have a command named helloworld so I can run the program from anywhere in the file system. I need something which can produce the following output.

$helloworld
 hello world

The gemspec file is as follows.

Gem::Specification.new do |s|
  s.name               = "helloworld"
  s.version            = "0.0.1"
  s.default_executable = "helloworld"

  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
  s.authors = ["Suranga"]
  s.date = %q{2015-08-06}
  s.description = %q{Hello World}
  s.files = ["Rakefile", "lib/helloworld.rb"]
  s.test_files = ["test/test_helloworld.rb"]
  s.require_paths = ["lib"]
  s.rubygems_version = %q{1.6.2}
  s.summary = %q{Hello World!}

  if s.respond_to? :specification_version then
    s.specification_version = 3

    if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
    else
    end
  else
  end
end

How can I do this?

Upvotes: 0

Views: 87

Answers (2)

matt
matt

Reputation: 79723

The default_executable setting is deprecated and doesn’t specify an executable anyway. You need to use executables instead:

s.executables << "helloworld"

Also have a look at the adding an executable section of the Rubygems guide.

Upvotes: 2

Felix
Felix

Reputation: 4716

Assuming a default setup.

Use rvm

First, I suggest you to install rvm (http://rvm.io). Then you wont need sudo permissions and can install gems in sane (testing, development) environments.

Use a tool to prepare your gem-work

Second, if you plan to implement a gem, I suggest you use bundler (and there are many probably superb alternatives) to give you a basic structure. If called like

bundle gem --bin helloworld

It will create the sketch of a helloworld gem for you, with a file in the directory exe that will be placed on your $PATH when installed - so that you can call it (see bundle help gem for a few more options). I dislike the new directory name (exe/ used to be bin/), but what bundler produces is still a very good and lean outline, I find.

During development you will find yourself playing around with it like this (then it will not use any "installed" version of the gem)

bundle exec exec/helloworld

Happy coding!

Upvotes: 0

Related Questions