linux-Ruby - run a ruby application as a command
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 bu开发者_C百科t 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.
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:
- move your files to a directory in the $LOAD_PATH list
give Ruby the path to your libraries in your require statement:
require '/home/mydir/myproject/lib/Data.rb'
modify $LOAD_PATH in Main.rb to include your lib directory
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
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
精彩评论