Basic Lessons on Programming in Ruby

Lesson 8 - Formatted Print
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 7
  4. The ability to figure things out!
---------------------------------------------
Lesson 8 - Formatted Print

This lesson uses a do loop from the last lesson, but concentrates on making the output look pretty. If you have data that needs to be written in columns, you usually don't use tabs like we did in the last lesson, due to problems with uneven columns. Instead you do like we show here - using a formatted print statement. Many programming languages provide some way to do this similar to how ruby does it.

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

    • #!/usr/bin/ruby
      puts "This program prints a number, its square, it's square-root "
      puts "and a random point result raised to a decimal power for a "
      puts "given range of values specified in a do loop."
      puts
      start = 1
      finish = 25
      iterations = finish - start + 1
      num = start
      # print heading line
      printf("%s , %s , %s , %s \n","number","square","square root","(number + 0.27)**2.1")
      iterations.times do
      printf("%d , %d , %0.05f , %0.05f \n",num,num**2,num**(1/2.0),(num+0.27)**2.1)
      num = num + 1
      end
      puts

  2. Now, make your program executable (chmod +x lesson8.rb) and run your program (./lesson8.rb) and look at the result. It should look like this (if you enter the same start, end, and increment values I did):

    • number , square , square root , (number + 0.27)**2.1
      1 , 1 , 1.00000 , 1.65192
      2 , 4 , 1.41421 , 5.59312
      3 , 9 , 1.73205 , 12.03789
      4 , 16 , 2.00000 , 21.08136
      5 , 25 , 2.23607 , 32.79460
      6 , 36 , 2.44949 , 47.23477
      7 , 49 , 2.64575 , 64.44990
      8 , 64 , 2.82843 , 84.48149
      9 , 81 , 3.00000 , 107.36617
      10 , 100 , 3.16228 , 133.13674

  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.

  5. Note how much better the numeric part of the output looks than in the last lesson. Now, we need to clean it up ust a bit more to make the numeric output line up nicely with the heading line at the top. This is very easy. 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 lesson8b.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 do loop."
      puts
      start = 1
      finish = 25
      iterations = finish - start + 1
      num = start
      # print heading line
      printf("%6s , %8s , %9s , %12s \n","number","square","square root","(number + 0.27)**2.1")
      iterations.times do
      printf("%6d , %8d , %11.05f , %20.05f \n",num,num**2,num**(1/2.0),(num+0.27)**2.1)
      num = num + 1
      end
      puts

  6. Now, make your program executable (chmod +x lesson8b.rb) and run your program (./lesson8b.rb) and look at the result. It should look like this (if you enter the same start, end, and increment values I did):

    • number ,   square , square root , (number + 0.27)**2.1 
      1 , 1 , 1.00000 , 1.65192
      2 , 4 , 1.41421 , 5.59312
      3 , 9 , 1.73205 , 12.03789
      4 , 16 , 2.00000 , 21.08136
      5 , 25 , 2.23607 , 32.79460
      6 , 36 , 2.44949 , 47.23477
      7 , 49 , 2.64575 , 64.44990
      8 , 64 , 2.82843 , 84.48149
      9 , 81 , 3.00000 , 107.36617
      10 , 100 , 3.16228 , 133.13674

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

  8. 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. Study the program code and the output and make sure you understand how it works.
Summary of Lesson 8 on Ruby Formatted Print:
  1. Create a new program using the lines given above and name it "lesson8.rb".
  2. Create a new program using the lines given above and name it "lesson8b.rb".
  3. Make the programs executable, run it, and make sure the output looks EXACTLY like the examples given.
  4. Notice how the formatted print statement can be use to produce nicely lined up columns of numeric data.
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 "lesson8ex1.rb" (cp lesson8.rb lesson8ex1.rb).
  2. Modify your lesson8ex1.rb program to ask the user to enter a start and an end value from the keyboard (like we asked for start and end values in our lesson6.rb program). Using these values, use a do loop to show the square and cube root of all the integers between those starting and ending values. Use the formatted print statement to arrange your output in nice looking columns.
Return to the main Lessons page
Return to Rejoice Always Home

Last updated on ... June 11, 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.