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.
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"
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'
精彩评论