Ruby cannot find the file in the directory - Error Enoent

All,

Just started Ruby and using Notepad++ for writing code but for some
reason, upon execution of the *.rb file, the command prompt give me the
attached error (please see the image attached).

The file is just a text file called ‘text.txt’, which is sitting in the
same folder as the ruby script file. Im confused…

Secondly, why is the answer ‘555’ to the registration question… I
think Im confused by the ‘11’, which I thougth either should be read an
a string or a normal number. Can anyone shed some light on this
please…

Cheers,

Sask Khan wrote:

All,

Just started Ruby and using Notepad++ for writing code but for some
reason, upon execution of the *.rb file, the command prompt give me the
attached error (please see the image attached).

The file is just a text file called ‘text.txt’, which is sitting in the
same folder as the ruby script file. Im confused…

You’ll need to run “ruby” from the same directory where ‘text.txt’ is
located.
-rp

Roger P. wrote:

You’ll need to run “ruby” from the same directory where ‘text.txt’ is
located.
-rp

Roger, what do you mean? Ruby.exe is sitting in C:\Program
Files\Ruby\Bin\ruby.exe, am I to copy the script and the txt file to
that folder in order to run this?

Sask Khan wrote:

Roger P. wrote:

You’ll need to run “ruby” from the same directory where ‘text.txt’ is
located.
-rp

Roger, what do you mean? Ruby.exe is sitting in C:\Program
Files\Ruby\Bin\ruby.exe, am I to copy the script and the txt file to
that folder in order to run this?

No.
Add the following line on the top of your script:
Dir.pwd
This will show you in which working directory ruby was started from.
Guessing from the screenshot I assume it will be C:\Program
Files\Notepad++.
Since you did not specify the path of test.txt, ruby will try to open a
file test.txt in that directory.
There are a couple of ways around:
a) Change the working directory before you start ruby. For example open
a command prompt, enter “cd /d g:\docs\l_ruby”, then run the script:
“ruby analyzer.rb”
b) Change the working directory after you start ruby. Add a line before
your “File.read…” line:
Dir.chdir(“g:/docs/l_ruby”)
That will change the working directory to the one that contains the
test.txt file.
c) Figure out how to change the working directory that Notepad++ starts
ruby in. I have no idea how to do this.
I hope this will get you back on the track again. I suggest you have a
look at http://en.wikipedia.org/wiki/Working_directory to learn what a
working directory is, if you don’t know already. It’s essential to know
when writing scripts.

Daniel,

Thanks much for the help & guidance. Thats a wonderful start and atleast
now I know where to look.

best,

Daniel F. wrote:

No.
Add the following line on the top of your script:
Dir.pwd
This will show you in which working directory ruby was started from.
Guessing from the screenshot I assume it will be C:\Program
Files\Notepad++.
Since you did not specify the path of test.txt, ruby will try to open a
file test.txt in that directory.
There are a couple of ways around:
a) Change the working directory before you start ruby. For example open
a command prompt, enter “cd /d g:\docs\l_ruby”, then run the script:
“ruby analyzer.rb”
b) Change the working directory after you start ruby. Add a line before
your “File.read…” line:
Dir.chdir(“g:/docs/l_ruby”)
That will change the working directory to the one that contains the
test.txt file.
c) Figure out how to change the working directory that Notepad++ starts
ruby in. I have no idea how to do this.
I hope this will get you back on the track again. I suggest you have a
look at http://en.wikipedia.org/wiki/Working_directory to learn what a
working directory is, if you don’t know already. It’s essential to know
when writing scripts.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs