bennybdbc
bennybdbc

Reputation: 1425

Linking to external files in ruby?

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.

Upvotes: 1

Views: 3756

Answers (2)

stask
stask

Reputation: 866

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'

Upvotes: 2

jordelver
jordelver

Reputation: 8432

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"

Upvotes: 6

Related Questions