Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of Java CLASSPATH in Ruby?

Tags:

ruby

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 ?

like image 650
JCLL Avatar asked Sep 25 '09 07:09

JCLL


People also ask

How do I find my Java classpath?

To check our CLASSPATH on Windows we can open a command prompt and type echo %CLASSPATH%. To check it on a Mac you need to open a terminal and type echo $CLASSPATH.

Is path the same as CLASSPATH?

The main difference between PATH and CLASSPATH is that Path is set for java tools in java programs like java and javac, which are used to compile your code. Whereas CLASSPATH is used by System or Application class loader to locate and load compile Java bytecodes stored in the . class file.

What is the default CLASSPATH in Java?

Setting the CLASSPATH can be tricky and should be performed with care. The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.

What is set CLASSPATH in Java?

CLASSPATH describes the location where all the required files are available which are used in the application. Java Compiler and JVM (Java Virtual Machine) use CLASSPATH to locate the required files.


2 Answers

  • 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.

like image 146
Peter Kofler Avatar answered Sep 23 '22 13:09

Peter Kofler


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"
like image 37
khelll Avatar answered Sep 22 '22 13:09

khelll