# File.open very basic

**URL:** <https://www.ruby-forum.com/t/file-open-very-basic/205615>\
**Category:** Ruby\
**Created:** [April 11, 2011, 1:52am UTC](https://www.ruby-forum.com/t/file-open-very-basic/205615 "2011-04-11T01:52:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 11, 2011, 1:52am UTC](https://www.ruby-forum.com/t/file-open-very-basic/205615/1 "2011-04-11T01:52:04Z")

</div>

Hello, I’m very new to Ruby, and trying to follow the examples in  
‘Beginning Ruby’ by Peter C.

However, I’m completely stuck right at the beginning, trying to work  
with File.open

I have

line\_count = 0  
File.open(“text.txt”, “r”).each { |line| line\_count += 1 }  
puts line\_count

text.txt is in the same folder as my ruby file, and is ~ 100 lines of  
text. However, line\_count always outputs 0 for me.

I’ve tried changing the syntax around, but I really have no clue what  
the problem may be.

---

<div class="post-metadata">

**Author:** ![kushboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kushboy](https://www.ruby-forum.com/u/kushboy)\
**Post date:** [April 11, 2011, 2:51am UTC](https://www.ruby-forum.com/t/file-open-very-basic/205615/2 "2011-04-11T02:51:32Z")

</div>

Hi,

Is your text source in the same directory as the ruby file you’re  
running?

I’m somewhat new as well, but copied/pasted what you had info a new ruby  
file, and it works fine here.

---

<div class="post-metadata">

**Author:** ![kushboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kushboy](https://www.ruby-forum.com/u/kushboy)\
**Post date:** [April 11, 2011, 3:30am UTC](https://www.ruby-forum.com/t/file-open-very-basic/205615/3 "2011-04-11T03:30:08Z")

</div>

Brett Kruger wrote in post #992039:

> line\_count = 0  
> File.open(“text.txt”, “r”).each { |line| line\_count += 1 }  
> puts line\_count
> 
> text.txt is in the same folder as my ruby file

very strange… what version of ruby are you running? this code  
copied and pasted works for me in 1.8.7 and 1.9.2. are you absolutely  
sure the .txt file is in the same directory as the program, and that  
it’s called “text.txt”? can’t think of any other reason it wouldn’t  
work…

good luck,

-j

---

<div class="post-metadata">

**Author:** ![kushboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kushboy](https://www.ruby-forum.com/u/kushboy)\
**Post date:** [April 11, 2011, 4:43am UTC](https://www.ruby-forum.com/t/file-open-very-basic/205615/4 "2011-04-11T04:43:15Z")

</div>

Ya I’m quite stumped as to what the problem could be. text.txt is  
definitely in the same folder as my program, and I am using 1.9.2.  
I tried uninstalling and reinstalling, but no luck there. I think I’ll  
try installing 1.8.7, and hopefully it will work there.

---

<div class="post-metadata">

**Author:** ![kushboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kushboy](https://www.ruby-forum.com/u/kushboy)\
**Post date:** [April 11, 2011, 5:03am UTC](https://www.ruby-forum.com/t/file-open-very-basic/205615/5 "2011-04-11T05:03:53Z")

</div>

So I found a strange solution -

I had .rb files set to open in my text editor by default, and would  
manually open them with the ruby interpreter to run them. The code  
worked fine when I switched it back to opening with ruby by default.

---

<div class="post-metadata">

**Author:** ![kushboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kushboy](https://www.ruby-forum.com/u/kushboy)\
**Post date:** [April 11, 2011, 3:02pm UTC](https://www.ruby-forum.com/t/file-open-very-basic/205615/6 "2011-04-11T15:02:38Z")

</div>

Brett Kruger wrote in post #992039:

> line\_count = 0  
> File.open(“text.txt”, “r”).each { |line| line\_count += 1 }  
> puts line\_count
> 
> […] line\_count always outputs 0 for me.

Change the “File.open(…” line to:

puts “Last modified:”, File.mtime(“text.txt”)  
puts “Size:”, File.size(“text.txt”)

and see if the output makes sense.

---

<div class="post-metadata">

**Author:** ![kushboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kushboy](https://www.ruby-forum.com/u/kushboy)\
**Post date:** [April 11, 2011, 10:52am UTC](https://www.ruby-forum.com/t/file-open-very-basic/205615/7 "2011-04-11T10:52:15Z")

</div>

Maybe your text editor doesn’t run it from the same location the files  
are  
stored in. When you say to open “text.txt” it is looking in your current  
directory, Dir.pwd, not the current directory of the file  
File.dirname( **FILE** ).

~/deleteme/file-test $ tree  
.  
|-- linecounter  
| |-- linecount.rb  
| `-- text.txt`– text.txt  
1 directory, 3 files

~/deleteme/file-test $ cd linecounter/

~/deleteme/file-test/linecounter $ ruby linecount.rb  
5

~/deleteme/file-test/linecounter $ cat text.txt  
1  
2  
3  
4  
5

~/deleteme/file-test/linecounter $ cd …

~/deleteme/file-test $ ruby linecounter/linecount.rb  
3

~/deleteme/file-test $ cat text.txt  
1  
2  
3

~/deleteme/file-test $ rm text.txt

~/deleteme/file-test $ ruby linecounter/linecount.rb  
linecounter/linecount.rb:2:in `initialize': No such file or directory - text.txt (Errno::ENOENT) from linecounter/linecount.rb:2:in`open’  
from linecounter/linecount.rb:2:in `’

~/deleteme/file-test $ tree  
.  
`-- linecounter |-- linecount.rb`– text.txt
