I am receiving the error No such file or directory in my command line and I think it's because I am not saving the ruby files somewhere specific. All I did was create a random folder where I would save my ruby files.
Do I need to save my scripts in the original ruby folder? Thanks!
** This is Windows 7.
More info -
All I did was make a simple file named "Matz.rb" because I'm currently reading the O'reilly Ruby book. In my code all I wrote was puts "Hello Matz". I saved this on my desktop. When I go to the command line it and I write ruby matz.rb it says "ruby: No such file or directory -- matz.rb " Please help :(
If this has something to do with PATH or shells, I honestly have no idea what those really are because I just started coding last night.
You are most likely not in the right folder. You somehow need to tell the ruby interpreter where it is looking for the file.
Either make sure you're in the right folder - the cd
command allows you to change location:
cd C:\Users\Username\Desktop
ruby Matz.rb
or specify the path explicitly:
ruby C:\Users\Username\Desktop\Matz.rb
By default, the ruby interpreter will look in your current directory (the location shown in your prompt) for whatever filename you give it.
Edit: I'll attempt to explain what I mean step-by-step.
When you initially open the command prompt, it will indicate what folder you are in (your "current working directory") in the prompt:
C:\Users\YourUsername >
In the above example, you are working in the C:\Users\YourUsername
folder.
- You can move directories using the
cd
command. For example, typing cd Desktop
moves you into the folder called Desktop
, assuming such a folder exists in your current location
- You can move to another folder outside your current folder by specifying explicitly where you want to be:
cd C:\Another\Place
- When you run a ruby command such as
ruby Matz.rb
, the system knows how to find the ruby
program because the installer placed its location into the PATH
environment variable. Don't worry about this too much, this just explains the "magic" by which it knows what ruby
means, no matter where you are.
- Ruby finds the file you specify (in the above example,
Matz.rb
) by looking in the current directory. To re-iterate, it is looking in whatever folder is written right there in your prompt.
- You can tell ruby to look outside the current folder by specifying the full path (as shown in the answer above).
To go from a new command window that you've just opened, to typing ruby Matz.rb
and having it work, you need to do the following:
- Move to the correct directory
- Run the command
If we assume your username is alex
and you have a folder on your desktop called "rubycode", which contains Matz.rb
, you could do this:
- Open a command prompt, which will most likely start in
C:\Users\Alex
- Move to the rubycode folder on your desktop:
cd Desktop\rubycode
. All subsequent commands will be working from within this folder.
- Run the
ruby
command, specifying the file: ruby Matz.rb
- Continue to run ruby commands as you learn ruby.
I hope that makes sense.