Basic Lessons on Programming in Ruby - Your First Ruby Program
What I assume you have:|
- A Linux / UNIX / Mac computer with Ruby installed. If you don't have this, you need to Google how to get Ruby installed to compile Ruby programs from the terminal or command line.
- The basic ability to run commands like ls , cp , vi (or some other command-line editor) , etc.
- The ability to figure things out!
Basic Output Lesson 1|
- First, use your text editor to type the following lines into a text file, exactly as you see them here. Save the file with the filename first.rb
puts "This is a ruby program."
puts "It is not very interesting..."
There are two ways to run this ruby program. The first is to simple type this at the command line (assuming you are in the same folder where you saved the file first.rb):
Do you see the results of your ruby program? It should look like this:
- This is a ruby program.
It is not very interesting...
Now, another way to run a ruby program in the Linux/UNIX/Mac world is to make your file an executable file. You do this by typing a command like this at the command line:
Now you can run your ruby program, contained in the file first.rb, by simply typing the following from the command-line (again assuming you are in the folder where the file exists. Be sure to press the 'Enter' key after typing the program name.
You should see the same program result that you saw when you ran the program earlier.
Summary of how to create and run a ruby program:|
- Type the program into a text file using vi or another suitable editor and save the file with .rb at the end of the filename
- Run the program by typing "ruby filename.rb" from the command-line.
- (or run the program by making it an executable file (chmod +x filename.rb) and typing "./filename.rb" at the command-line.
Now, if you undertood all this you should be able to do the following exercises:|
- Write a ruby program that prints any text you want. Just put a different line of text in the quotes on the print "" statement in the program. Name your program "second.rb". This isn't a hard task to do, but you need the experience of creating a progam from scratch and running it to make the rest of the lessons easy. Take time to do it!
- Write a ruby program to print something you want to print, just for fun. Name your program "third.rb". For more interesting variety on controlling print output, see the next lesson!
Last updated on ... May 15, 2012
Created on ... May 15, 2012
These lessons were created by David Joyner. All rights reserved. You may use them to learn Ruby or teach Ruby to others as long as you DO NOT CHARGE for these materials! For any other use, permission may be asked of David Joyner at email@example.com.