tags:

views:

52

answers:

2

Hi,

I want to run a lib of ruby files from the command prompt from anywhere in a command line. I have the Main.rb program which instantiates classes from other ruby files.

I store the class path of my lib in the .zshrc. Then I run the Main.rb but it is not able to load the required ruby files (files in my lib folder) and throws this error:

`require': no such file to load -- Data.rb (LoadError)

How can I tackle this issue? I just need a neat command to be run on shell and throw results on console.

Please help.

+1  A: 

Ruby searches the directories in the $LOAD_PATH variable to try to find files you import with the 'require' statement. You have a few options:

  1. move your files to a directory in the $LOAD_PATH list
  2. give Ruby the path to your libraries in your require statement:

    require '/home/mydir/myproject/lib/Data.rb'

  3. modify $LOAD_PATH in Main.rb to include your lib directory

  4. pass Ruby a command-line argument adding your lib directory to $LOAD_PATH with the -I option:

    ruby -I/home/mydir/myproject/lib Main.rb

Seamus Campbell
Hey $: << '.' unless $:.include? '.' doesnot work plus how can I just use it as a normal command say test or just Main without any .rb thing...I m planning to use the third option above but still dono how to include this in my code.
A: 

Typically you'll need to change your

 require 'abc'

to a relative path, like

 require File.dirname(__FILE__) + "/abc"

or use (my) require_relative on 1.9

 require_relative 'abc'

GL. -r

rogerdpack