views:

145

answers:

2

I seem to be having trouble with Rails finding the appropriate path to a textfile I would like to load into memory for processing, for a personal learning project.

require "#{RAILS_ROOT}/lib/english.txt"

or

require "/lib/english.txt"

are not working.

The textfile is located in the lib directory.

I am trying to load it from a simple controller. I continue to get errors:

MissingSourceFile in EntriesController#anagram
no such file to load -- /Users/me/Sites/cvtest/lib/english.txt

The above path is actually where the file is located according to my computer's info (OSX). What is it that I am doing incorrectly? Thank you.

+2  A: 

If the file contains Ruby code, change the extension to rb. Chances are rails will just load the file automatically without any require being necessary. At worst require 'english' or require 'lib/english' should be all you need.

require is for loading Ruby code extensions, libraries and what-have-you.

If english.txt doesn't contain valid Ruby code, then require isn't what you're looking for, there's nothing to see here, move along. Investigate the many wonders of the File class.

Mike Woodhouse
thank you. I'll look it up. It is just a .txt file of words with no ruby code in it. I'll slow down and pay attention :)
cloudhead
+2  A: 

File.open("#{RAILS_ROOT}/lib/english.txt") should do it.

require is for including Ruby code, such as including a Module in a Class.

Sarah Mei
cloudhead