# Where to put a file in io.foreach?

**URL:** <https://www.ruby-forum.com/t/where-to-put-a-file-in-io-foreach/91514>\
**Category:** Rails\
**Created:** [April 10, 2007, 1:46am UTC](https://www.ruby-forum.com/t/where-to-put-a-file-in-io-foreach/91514 "2007-04-10T01:46:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [April 10, 2007, 1:46am UTC](https://www.ruby-forum.com/t/where-to-put-a-file-in-io-foreach/91514/1 "2007-04-10T01:46:01Z")

</div>

Hi there,

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”.

Any ideas? This is the first time I’ve tried this!

Dave

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [April 10, 2007, 2:17am UTC](https://www.ruby-forum.com/t/where-to-put-a-file-in-io-foreach/91514/2 "2007-04-10T02:17:12Z")

</div>

> 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 🙂

Sincerely,  
Chuck V.
