0

I have this at the top of the file:

parser_spec.rb:

require File.dirname(__FILE__) + '/parser.rb'

Which has a class inside:

parser.rb:

class Scraper
  def scrape_department(file)

Both files are in the rails/project folder.

How to do it so that I can include that file by just doing require 'parser'?

Shoe
  • 74,840
  • 36
  • 166
  • 272
alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • 1
    If you are using Rails and this file is in the lib directory, you might simply load all your libs file using `config.autoload_paths += %W(#{config.root}/lib)` in `config/application.rb` – Pierre-Louis Gottfrois Aug 16 '13 at 07:21

2 Answers2

0
File.dirname(__FILE__)

Just means to look for the file in the directory of the caller.

Try using

require_relative 'parser'

By the way, I would suggest renaming your file scraper.rb since this is the name of your class inside that file.

Pierre-Louis Gottfrois
  • 17,561
  • 8
  • 47
  • 71
  • Yes, I renamed it scraper.rb. But I followed your suggestion and I got `/home/alex/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- scraper (LoadError) from /home/alex/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubyg`. Both files are in the rails/project folder. – alexchenco Aug 16 '13 at 06:42
  • According to the documentation here http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-require_relative, the file path cannot be determined – Pierre-Louis Gottfrois Aug 16 '13 at 07:19
0

I think you are looking for require_relative:

require_relative 'parser.rb'

From the docs:

Ruby tries to load the library ... relative to the requiring file’s path.

Stefan
  • 109,145
  • 14
  • 143
  • 218