views:

91

answers:

2

Sorry if this question is very easy, but I can't find the answer anywhere. How do you refer to an external ruby file in a ruby script if, for example, the file you want is in the same folder as the one you are writing? Thanks in advance.

+5  A: 

You just do a require like this require "filename". Ruby will look in each of the paths in the $LOAD_PATH variable to find the file.

Have a look at $LOAD_PATH and you should get something like this:

irb(main):001:0> puts $LOAD_PATH
/usr/local/lib/ruby/site_ruby/1.8
/usr/local/lib/ruby/site_ruby/1.8/i686-darwin10.0.0
/usr/local/lib/ruby/site_ruby
/usr/local/lib/ruby/vendor_ruby/1.8
/usr/local/lib/ruby/vendor_ruby/1.8/i686-darwin10.0.0
/usr/local/lib/ruby/vendor_ruby
/usr/local/lib/ruby/1.8
/usr/local/lib/ruby/1.8/i686-darwin10.0.0
.

You can see that the last entry is the current directory.

You can also give a path specifically like require "lib/filename.rb"

jordelver
So, if it's in the same directory: `require './filename.rb'`
glenn jackman
That will work as would require "filename.rb" or require "filename"
jordelver
+2  A: 
require "YOUR_EXTERNAL_FILE_NAME_WITHOUT_DOT_RB_EXTENSION"

For example, if you have two files, 'file0.rb' and 'file1.rb' and want to include 'file0.rb' from 'file1.rb' (and both files are in the same folder), your 'file1.rb' should have following statement:

require 'file0'
stask