0

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
  • Try putting this at the top of Main.rb: $: << '.' unless $:.include? '.' Commented Aug 1, 2010 at 18:48

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.