Basic Lessons on Programming in Ruby

Lesson 6 - For Loops
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 5
  4. The ability to figure things out!
---------------------------------------------

Lesson 6 - For Loops

First, I know that Ruby really does not have, nor does any normal Ruby programmer normally use a classic "for loop". Now, having said that, let me just plead the excuse that mathematicians who work with numeric programming use the classic for-loops on a regular basis. So, please, real Ruby programmers, bear with me and endure this little diversion.
A classic for-loop is one in which you specify the number of times the loop is to be performed (or, in a typical mathematician's terminology, "iterated"). In this lesson, we will show how to do this in Ruby.

  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 lesson6.rb

    • #!/usr/bin/ruby
      puts "This program prints a number, its square, it's square-root and a random"
      puts "point result raised to a decimal power for a given range of values "
      puts "specified in a for loop."
      puts
      for num in (1..5)
      print num , " , " , num**2 , " , " , num**(1/2) , " , " , (num+0.27)**2.1 , "\n"
      end
      puts "------------------------------------------"
      for num in (13..16)
      print num , " , " , num**2 , " , " , num**(1/2.0) , " , " , (num+0.27)**2.1 , "\n"
      end
      puts

  2. Now, make your program executable (chmod +x lesson6.rb) and run your program (./lesson6.rb) and look at the result. It should look like this:

    • This program prints a number, its square, it's square-root and a
      random point result raised to a decimal power for a given range
      of values specified in a for loop.

      1 , 1 , 1 , 1.65191544601299
      2 , 4 , 1 , 5.59312209818106
      3 , 9 , 1 , 12.0378875076265
      4 , 16 , 1 , 21.0813557728505
      5 , 25 , 1 , 32.7945965130094
      ------------------------------------------
      13 , 169 , 3.60555127546399 , 228.049402136907
      14 , 196 , 3.74165738677394 , 265.638086722531
      15 , 225 , 3.87298334620742 , 306.240043522693
      16 , 256 , 4.0 , 349.875765089772


  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 6 on Ruby For Loops:
  1. Create a new program using the lines given above and name it "lesson6.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 how to perform an operation or a process a specified number of times. There is also a good example of using the tab character ("\t") for even spacing of elements in columns. We will also emphasize the importance between integers and floating point numbers in the first exercise below. Notice how output looks in the second for-loop (using tabs) compared to the second (using commas). The tabs help to make the output less cluttered.
Now, if you understood all this you should be able to do the following exercises:
  1. Make a copy of your program above and name it "lesson6ex1.rb" (cp lesson6.rb lesson6ex1.rb).
  2. Modify your copy to use the numbers 1 through 5 for the first for-loop and 6 through 10 for the second. Change the line in the first for-loop where it says "num**(1/2.0)" to "num**(1/2)" and look at the output. Not what you expected, is it? One way to calculate the square root of a number is to raise it to the 1/2 or 0.5 power. But, in Ruby, if you use 1/2, Ruby will perform the calculation using integer division, and think like this: "2 will go into 1 zero times" and will give a result of zero. Then, anything raised to the zero power is one, explaining the results in the first for loop. In Ruby, if you want to calculate 1/2 and get a decimal result of 0.5, you will need to tell Ruby to perform what is called in computer terms as "floating point arithmetic" by writing your calculation with either the 1 or the 2 written with a decimal point. Either write 1.0/2 or 1/2.0 or 1.0/2.0. Try to remember this if you are writing a program in Ruby that involves mathematical calculations.
  3. Use the lesson6.rb program as a model and write a program named lesson6-ex2.rb that will print the cubes and cube roots of the numbers from 1 to 20.
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 david.joyner@lcu.edu.