tags:

views:

147

answers:

2

I would like to run ruby programs from anywhere. I think I have understood it is RUBYLIB. But I can't make it work. Could you give examples ?

Thx JC

+1  A: 

There is an option -S which looks for the script using PATH environment variable.

for example doing:

ruby -S some_script

Will look for the some_script in current operating system PATH environment variable.

Update: If your script requires other files then use the following statement:

require File.join(File.dirname(__FILE__), "name_of_required_file")

instead of:

require "name_of_required_path"
khelll
Ok, the aforementioned file "some_script" is now correctly found by ruby (thx!), but this file also contains a 'require 'foo'", that is not loaded (LoadError). It resides in the same directory as the first script...Any idea ?
JCLL
To get your required file included you can do the followingrequire File.join(File.dirname(__FILE__), "name_of_required_file")
Steve Weet
The comment code remove 2 underscores before and after the file it should be xxFILExx where x is an underscore
Steve Weet
Yes Steve's solution should work. I have updated the post to have it.
khelll
+3  A: 
  • You need to manupulate the load path $LOAD_PATH ($:)
  • This is done with -I directories (Directories are separated by a : on Unix-like systems and by a ; on DOS/Windows systems.)
  • You could add -I switches to RUBYOPT ($SAFE must be 0)
  • Or with RUBYLIB ($SAFE must be 0 also) which contains search paths.
  • RUBYPATH also changes search path for Ruby programs.
  • For environment variables, make sure they are proper set or exported so the Ruby VM sees them. You could add a debug print in the ruby.bat or ruby.sh.
  • Check your $SAFE setting. If you don't know about it, then its probably fine.

I allways set RUBYLIB and RUBYPATH to my loadpath and add the -S option to the interpreter call.

Peter Kofler