How do I save a ruby file? (error) - ruby-on-rails

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.

Related

How to move a file 'to a directory that's on your PATH'?

Im trying to install a JSON formatter for Cucumber but am having trouble configuring it. The steps (listed here) go like this:
1. Download cucumber-json-formatter-darwin-amd64 and rename it to cucumber-json-formatter
2. Move it to a directory that's on your PATH
3. Make it executable with chmod +x cucumber-json-formatter
4. Verify that you can run it: cucumber-json-formatter --help
I have the file downloaded and renamed correctly. However, I am stuck on the second step of moving it to a directory thats on my PATH.
Doing some research, I know what the folder structure looks like but I'm not sure exactly what the step is instructing. How would I achieve this step? Can it be in ANY directory on my PATH? I am currently using a Mac if that makes any difference for the solution.
Move it to a directory that's on your PATH
PATH refers to the machine's environment variable named PATH. Any time the OS is asked to execute something PATH is searched.
On Windows open System Properties dialog, click Environment Variables button and Path is listed there. You can add a new entry for the location of cucumber-json-formatter or you can move it to an existing Path entry.

Ruby change directory in code

So, I've came across an issue where I delete the current folder where my ruby script is executing ex:
/home/user/scriptfolder
Now i have my ruby gem running and I do this:
mycommand --deletefull
now mycommand is an GLI command, which should delete the content of the folder and the folder itself. I do that by using my custom class:
ClientModuleDir.rm_f(path)
now after deleting it I want to just do cd ..
however I've tried several methods:
system('cd ..')
Dir.chdir(dir) #dir is abs path without the scripfolder name, i've tried every combination with this command, nothing works so far.
However these methods are not working.
I still am in the
/home/user/scriptfolder
after executing these commands, but the folder DOES NOT EXIST. When I manually do cd .. and i do ls the folder is not there.
How do I change "physically" the folder in ruby code ?
The current working directory is always kept for the current program only. Changing the working directory in a program won't affect any other running programs, including its parent.
Thus, when you delete the directory in your Ruby script and change the working directory of the Ruby process one level down, this won't affect the shell process which has started your Ruby script.

LoadError when trying to get source code in Ruby

Trying to teach myself Ruby and using the Book Apress Beginning Ruby. I bought TextMate 2 and in the very beginning of Chap. 4 I am being asked for following:
Launch Terminal (Completed)
Use cd to navigate to the folder where you placed example1.rb, like so:cd ~/ruby. This tells Terminal to take you to the ruby folder located off of your home user folder. (Can't figure this out. What is "cd". Where do I type this TextMate 2 or Terminal. Do I type irb prior? I saved the example1.rb on my desktop, tried to drag and drop, says permission denied).
Type ruby example1.rb and press Enter to execute the example1.rb Ruby script.
If you get an error such as ruby:No such file or directory -- example1.rb (LoadError), you aren’t in the same folder as the example1.rb source file, and you need to establish where you have saved it. (Obviously I get this error, but I can't complete step 2).
Was super excited getting through chap 1-3 and using irb and now I bought TextMate2 and I get stuck trying to start my first ruby app.
Can anyone help explain Step 2 so I can complete 3 and 4?
What is "cd"
cd is change directory command. Type it in your terminal like this:
cd /dir/that/has/example1.rb
If example1.rb is in your Desktop and you are using Mac and your username is tom, then cd command would be:
cd /Users/tom/Desktop
Then from the terminal again, type:
ruby example1.rb
to run your Ruby script.

Ruby on Rails -- new install on windows 7 but cant run ruby commands, problem with path?

I just installed ruby on rails on my new computer ( I was using instantrails before) and I'm trying to get everything setup.
Im running Windows 7. So I followed the instructions from this tutorial.
http://blogupstairs.com/ruby-on-rails/installing-ruby-on-rails-on-windows-7/
The problem i'm guessing is step 3
"Add the newly installed bin directory to your path in your windows system : Open windows explorer-> right click the icon computer-> choose Properties -> in the contol panel Home, Click Advanced system settings and then click Envitonment Variables button->in the system variables click new and add new system variables like this : Variable name : RUBY_HOME , Variable Value : C:\Ruby, after that add it in to the path and add the bin after a semicolon to the “Path” variable like this : C:\Program Files\Common Files\Adobe\AGL;%JAVA_HOME%\bin;%ANDROID_HOME%\tools;%RUBY_HOME%\bin"
I set created the system variables but when I try to run script/generate in the main directory of my app I get the error
"Ruby: no such file or directory --script/generate"
I checked the apps directory and the script folder is in there. How can I run the "ruby script/generate" commands from the control panel?
I was guessing it was something with the path but I dont know any ways to check to find out whats going wrong.
Are you using Rails 3.0+? In this version of rails and up, the script/* folder has been deprecated in favor of rails generate, rails server, etc. See here.
In 99.99999% of all cases, if a computer tells you that it cannot find a file at a specific location, it is because that file is not at that specific location.
So, is there actually a file named generate in a directory named script in the current directory?
I have recently installed RoR on my "clean" Win7 as well.
What IDE are you using?
I suggest trying JRuby with NetBeans/RubyMine:
JRuby installer does all the work (even adds the proper variables to your path in system environment settings...,
IDEs take care of gems, setting up servers etc.
As for the commands, as Nuclearsandwitch mentioned, there is no script/generate or script/server in Rails 3. Just make sure you in the directory with your Rails app and then try running rails server. It should work :-)

"rails" command on windows

So I installed Ruby On Rails using the Windows Installer.
Now the startup guides says I have to start up an Terminal and run the Rails command to make a project. I don't have a terminal, so how do I execute a Rails command and make a project??
The CMD command line in Windows does not recognize "rails"
I don't think it is an PATH problem, because when I'm in the "Bin" directory, there is "rails.bat" and "rails" with no extension. If I run Rails.bat I get "The filename, directory name, or volume label syntax is incorrect."
I get that message with whatever Argument I pass to it.
It seems to be a bug in the rails.bat file because of double-quotes, like:
#"ruby.exe"" "%~dpn0" %*
Remove the double quotes:
http://sepitfalls.blogspot.com/2008/12/updateing-rubygems-broke-gem-command.html
To get a terminal
Start > Run... or Windows key + R
Type "cmd" and hit enter.
To add a folder to the command line's path
On Windows XP (Vista is similar)
Start > Control Panel > System
Click the "Advanced" tab
Click "Environmental Variables"
Under "System Variables" double click on Path and edit accordingly.
You will need to open a new command line in order to get the updated path to take affect. With XP, I've had to reboot to force it to take affect but it shouldn't be necessary.
The Path variable is a semi-colon seperated list of folders in which the Command Line or "terminal" looks for commands.
You'll probably have to find the rails command yourself. I would try looking for a rails.bat or rails.cmd file.
I think they mean the command window. Try running it from a command line.
I dont know where the Windows Ruby on Rails intaller places its files, but the ultimately solution is that you need to make sure that the "rails" command is is your PATH - which you can view/modify by right-clicking My Computer, selecting Properties, going to the Advanced tab and then clicking on "Environment Variables" and then scrolling down until you see "PATH" and adjusting it accordingly. You probably need to find the directory where the "rails" command lives and append to the end of the existing PATH entry, inserting a ";" before you enter the new path to append it to the existing PATH.
So find where "rails" is and add its directory to your PATH.
You probably have to change your PATH in Windows to point to wherever the rails executable has been installed.
The PATH environment variable is used to determine where Windows looks for stuff; when you type, for instance, 'calc' Windows will hunt through its PATH and eventually find and run C:\Windows\calc.exe. In your case, the PATH doesn't include the directory you need, so Windows isn't searching in that directory for rails.exe when you type "rails."
To fix this, right click on "My Computer" -> "Properties" -> "Advanced" -> "Environment Variables" and then modify the PATH one to include the directory your rails.exe is in.
I recommend using cygwin for Rails development on windows - far too much of the community is using *nix. If you install the development tools package you'll automatically have svn, git and other doo-dads that you'll eventually need relating to various rails plugin installation. If you want to deploy using capistrano the installation is identical to all the instructions you find. Plus other tips and tricks people will reference will work exactly the same for you as it does for them, whereas some things are different for native windows and all the accessory tools have to be installed separately.

Resources