Basic Lessons on Programming in Ruby

Lesson 5 - Keyboard Input
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 4
  4. The ability to figure things out!
---------------------------------------------

Lesson 5 - Keyboard Input
  1. There are three classic ways for your program to get input from a user. The first method is for your program to ask for what it needs. This example illustrates this method. 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 lesson5-keyboard-input.rb

    • #!/usr/bin/ruby
      puts
      puts "Type in your name and press the 'Return' key: "
      name = gets.chomp
      puts
      print "Type in a number and press the 'Return' key: "
      a = gets.chomp
      print "Type in another number and press the 'Return' key: "
      b = gets.chomp
      puts
      print "Type in another number and press the 'Return' key: "
      c = gets.chomp
      puts
      print "Type in another number and press the 'Return' key: "
      d = gets.chomp
      c = c.to_i
      d = d.to_i
      puts
      puts "Hello, #{name}, here are some math results for your numbers:"
      puts
      puts "The first number you entered was #{a}"
      puts "The second number you entered was #{b}"
      puts "The third number you entered was #{c}"
      puts "The fourth number you entered was #{d}"
      puts
      print "The sum of the first two numbers is: "
      print a + b
      puts
      print "The sum of the second two numbers is: "
      print c + d
      puts
      puts

  2. Now, make your program executable (chmod +x lesson5-keyboard-input.rb) and run your program (./lesson5-keyboard-input.rb) and look at the result. It should look like this (unless, of course, you enter a different name and numbers!):

    • Type in your name and press the 'Return' key:

      DJ

      Type in a number and press the 'Return' key: 3

      Type in another number and press the 'Return' key: 4

      Type in another number and press the 'Return' key: 5

      Type in another number and press the 'Return' key: 6

      Hello, DJ, here are some math results for your numbers:

      The first number you entered was 3

      The second number you entered was 4

      The third number you entered was 5

      The fourth number you entered was 6

      The sum of the first two numbers is: 34

      The sum of the second two numbers is: 11


  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. Study the program and its output carefully. It shows how to ask a user for input from the keyboard and store it in a variable. It also show how to use the input in some simple calculations. Notice the difference in the output in the last two lines. Can you figure out why in one case the two numbers were actually added together as expected and in the other case the two numbers were simply printed together? In programming terms we would say the contents of the variables in the next to the last line were concatenated.

Summary of Lesson 5 on Ruby Input:
  1. Create a new program using the lines given above and name it "lesson5-keyboard-input.rb".
  2. Make the program executable, run it, and make sure the output looks EXACTLY like the example given.
  3. Notice how the gets and print statements are used to perform the indicated calculations and the difference in the output of the program in the last two lines.
  4. The important new idea shown in this lesson is how to use the gets statement to allow the user to give input data to the program. Make sure you understand how to ask the user for information to be stored in variables, which your program can then process in whatever way needs to be done. In the next lesson you will learn more about why the output is different in the last two lines, about the chomp part of the gets statement and about the ".to_i" part of the program.
Now, if you underatood all this you should be able to do the following exercises:
  1. Make a copy of your program above and name it "lesson5ex1.rb" (cp lesson5-keyboard-input.rb lesson5ex1.rb).
  2. Modify lesson5ex1.rb to just work with two input numbers (not four) and to just show the result of multiplying the two numbers together. Delete the lines that show the concatenating result. If you find yourself having trouble with all this, do as much as you can and then email your instructor for help.
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.