Where to put a file in io.foreach?

I want a piece of code in my controller to grab a text file with a list of words in it, but I don't know where to put that text file (words.list).

Here's the relevant code:

  def puzzle_generator     # Load all 3-17 letter words into a hash for fast lookup     words = Hash.new     IO.foreach("words.list") do |line|

Where should I put words.list? I tried writing out a path to it, but I keep getting "No such file or directory - words.list".

words.list should be in the same directory as the .rb that's calling it. If that doesn't work and using the absolute path doesn't work try a relative path like ../../words.list if it's in your rails_root. In addition the following code might change things or not, I doubt it will make it work but it's a little prettier.

Try the following code: File.open("words.list").each do |line|   # Do something with the var line end

or a slightly different approach

words = File.readlines("words.list")

I'm not sure why but IO and foreach seem to be pretty rarely used. I think they've gone out of style. Maybe just because File is easier to get the gist of when dealing with Files and IO when doing io streams like tcp/ip.

Hope that helps. If you don't already have it in your bookmarks try to find _why's Pickaxe on google; it's an old copy of the Ruby manual with links to the sections. Got me through a lot of pain :slight_smile:

Sincerely, Chuck Vose