6

i want to execute my ruby script as executable file and also i should execute at /usr/bin/ directory. I know it is possible like this.

#!/usr/bin/ruby
puts "hello"

And

chmod +x hello

But I also want to require some ruby file.

For example if I add

require './other_ruby_script' 

into my codes and I move the Ruby executable file to /usr/bin/, it gives me error for:

cannot load such file 'other_ruby_script'

I want to execute the Ruby file at /usr/bin directory.

So maybe I should compile it? But I couldn't compile because i didn't understand when google searches "How to compile?".

How can i create executable ruby code as suitable format for my codes. (require './other_file'). And i don't have to execute like this ./hello my executable file. Just i should execute as hello

poseid
  • 6,986
  • 10
  • 48
  • 78
deniz
  • 155
  • 2
  • 3
  • 9

4 Answers4

4
#!/usr/bin/env ruby
require_relative 'other_ruby_script'
puts "hello"
Alexey
  • 9,197
  • 5
  • 64
  • 76
2

I think you ask how to configure the right loadpath. First, in your script I would do a:

puts $:

This should print whether you are loading the right Ruby environment (might be a problem if you are using rbenv or rvm). For example I get:

/Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/site_ruby/1.9.1
/Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/site_ruby/1.9.1/x86_64-darwin11.3.0
/Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/site_ruby

As long as your loadpath does not contain the directory with the script 'other_ruby_script' you will get this error:

LoadError: cannot load such file -- other_ruby_script

So, you should try to add the load path with:

$:.unshift "#{File.dirname(__FILE__)}/../some_path"

If you are not loading the Ruby environment in the first place, your line:

#!/usr/bin/ruby 

needs to be setup to load the environment from Rbenv or Rvm

Community
  • 1
  • 1
poseid
  • 6,986
  • 10
  • 48
  • 78
  • May be If i compile my ruby script, is it more easy? Because i work on a ruby project. It has many other ruby file. – deniz Feb 06 '13 at 20:13
  • Google does not give a result on the subject, because Ruby works with a Virtual Machine that compile code dynamically, like Java, Python or Perl. There are no compilers as far as I know. You need to learn how to load dependencies dynamically when working with Ruby. – poseid Feb 07 '13 at 09:12
1

You can write a shell script from where you execute the ruby file. You will need to export PATH and GEM_HOME in the shell script. The shell script will be like this:

#!/bin/bash
export PATH = /usr/local/rvm/gems/ruby-1.9.3-p194
export GEM_HOME = /usr/local/rvm/gems/ruby-1.9.3-p194
cd /path/to/ruby_script
ruby file_name.rb

You will get PATH and GEM_HOME by the following commands:

echo $PATH
echo $GEM_HOME 
Kumar Akarsh
  • 4,954
  • 2
  • 20
  • 31
0

The question is for a linux environment but this answer may help a Windows user who stumbles upon this in the future.

There are some packaging Gems for Ruby available here.

The most popular one is OCRA (One Click Ruby Application), which is available as both a Gem and a standalone Ruby file, and will package your Ruby project into a Windows executable.

zpr
  • 2,886
  • 1
  • 18
  • 21