What Really is Happening?

Hello, I need your help.
I just made the file "analyzer.rb" and placed on a folder.
This file has the code:

line_count = 0
File.open("text.txt").each { |line| line_count += 1 }
puts line_count

I tried to run it by CMD, but it doesn't oppened the "text.txt" file.
"analyzer.rb" and "text.txt" are on the same folder.

I am sending the CMD error that shows when I press to execute the
"analyzer.rb" file.

Could someone help me?

Thank you very much!

Attachments:
http://www.ruby-forum.com/attachment/7702/CMDerror.jpg
http://www.ruby-forum.com/attachment/7703/analyzer.rb
http://www.ruby-forum.com/attachment/7704/text.txt

···

--
Posted via http://www.ruby-forum.com/.

it looks like you're executing the script from a different directory than it exists in. Try cd-ing into the script's directory before executing it, or put an absolute path to text.txt into your script.

Look up how the current working directory of a process is set.

···

On Aug 28, 2012, at 9:46 PM, Pepe Pew <lists@ruby-forum.com> wrote:

Hello, I need your help.
I just made the file "analyzer.rb" and placed on a folder.
This file has the code:

line_count = 0
File.open("text.txt").each { |line| line_count += 1 }
puts line_count

I tried to run it by CMD, but it doesn't oppened the "text.txt" file.
"analyzer.rb" and "text.txt" are on the same folder.

Hello Jam, Andriy Andreyev and Robert Klemme, thank you for this help.
The file was really on same fold.
The only thing I did it was change the file "text.txt" for "file.txt" of
the file name and on the ruby cody, then it worked :slight_smile:
I don't understood why "text.txt" was not working, but what matters is
that now it worked.
Thank you a lot!

···

--
Posted via http://www.ruby-forum.com/.

Check whether the text.txt really sits in the same directory. It doesn't
seem like that.

The important bit here is to execute the script from the directory
where text.txt sits since the path is hardcoded in the script.

Btw, in Ruby 1.9.3 you can do

line_count = File.foreach("text.txt").count

:slight_smile:

Kind regards

robert

···

On Wed, Aug 29, 2012 at 5:58 AM, Jam <jam@jamandbees.net> wrote:

On Aug 28, 2012, at 9:46 PM, Pepe Pew <lists@ruby-forum.com> wrote:

Hello, I need your help.
I just made the file "analyzer.rb" and placed on a folder.
This file has the code:

line_count = 0
File.open("text.txt").each { |line| line_count += 1 }
puts line_count

I tried to run it by CMD, but it doesn't oppened the "text.txt" file.
"analyzer.rb" and "text.txt" are on the same folder.

it looks like you're executing the script from a different directory than it exists in.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/