|Basic Lessons on Programming in Ruby
Lesson 3 - Basic Variables and a Little More on Output
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.
- You have completed lesson 2
- The ability to figure things out!
Lesson 3 - Basic Variables With a Little More on Output|
- 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 lesson3.rb
x = 3.0
y = 4.0
z = (x**2 + y**2)**(1.0/2)
puts "This program demonstrates some Pythagorean Triples"
print "Let x = "
print " and let y = "
print "If you let z = "
puts "Then these numbers are a Pythagorean Triple, notice: "
print " + "
print " = "
Now, make your program executable (chmod +x lesson3.rb) and run your program (./lesson3.rb) and look at the result. It should look like this:
This program demonstrates some Pythagorean Triples
Let x = 3.0 and let y = 4.0
If you let z = 5.0
Then these numbers are a Pythagorean Triple, notice:
9.0 + 16.0 = 25.0
If anything looks different when you run your program, then check VERY carefully that you typed your program EXACTLY like the instructions above!!!
If you just can't get your program to produce the same output as what I said it should look like, you probably need to email your instructor and ask him why before you proceed to the exercises below.
Summary of Lesson 3 on Basic Ruby Variables:|
- Create a new program using the lines given above and name it "lesson3.rb".
- Make the program executable, run it, and make sure the output looks EXACTLY like the example given.
- Notice how the puts and print statements are used to make the output of the program easy to read and understand.
- The important new idea shown in this lesson is the use of variables to hold information. There are really many things going on here that need discussing at some point, but now is not the time. Just make sure you understand how the x and y variables are used to store the given values and how z is used to store the result of the square root of x^2 + y^2. Notice the particular way you had to use Ruby's math operators (the plus operator ( + ) , the exponent operator ( ** ), and the division operator ( / ) to get the calculation done. Finally notice the way we entered the numbers, some with decimals and some not. It is important to know what you are doing here and we will illustrate this in the next lesson. For right now, in the exercises you are given below, just try to imitate your example here.
Now, if you undertood all this you should be able to do the following exercises:|
- Make a copy of your program above and name it "lesson3ex1.rb" (cp lesson3.rb lesson3ex1.rb).
- Modify your copy to use the numbers 6 and 8 for x and y, which should be another Pythagorean Triple. Does you program work correctly and give the expected result "6^2 + 8^2 = 10^2"? If not, experiment with carefully changing little things to finally make it work.
- In the next lesson we will explore these mathematical things further, because they are important if you need to do anything with mathematical calculations in a Ruby program.
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.