Basic Lessons on Programming in Ruby

Lesson 3 - Basic Variables and a Little More on Output
What I assume you have:
  1. 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.
  2. The basic ability to run commands like ls , cp , vi (or some other command-line editor) , etc.
  3. You have completed lesson 2
  4. The ability to figure things out!

Lesson 3 - Basic Variables With a Little More on Output
  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 lesson3.rb

    • #!/usr/bin/ruby
      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 x
      print " and let y = "
      puts y
      print "If you let z = "
      print z
      puts "Then these numbers are a Pythagorean Triple, notice: "
      print x**2
      print " + "
      print y**2
      print " = "
      print z**2

  2. 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

  3. If anything looks different when you run your program, then check VERY carefully that you typed your program EXACTLY like the instructions above!!!

  4. 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:
  1. Create a new program using the lines given above and name it "lesson3.rb".
  2. Make the program executable, run it, and make sure the output looks EXACTLY like the example given.
  3. Notice how the puts and print statements are used to make the output of the program easy to read and understand.
  4. 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:
  1. Make a copy of your program above and name it "lesson3ex1.rb" (cp lesson3.rb lesson3ex1.rb).
  2. 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.
  3. 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.
Return to the main Lessons page
Return to Rejoice Always Home

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