Basic Lessons on Programming in Ruby

Lesson 2 - A little more on basic 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 1
  4. The ability to figure things out!
---------------------------------------------

Basic Output Lesson 2
  1. First, make sure you are in the same folder you were when you created the program first.rb in lesson 1. Then, use the cp command to make a copy of your program named first.rb from lesson 1. Name the new program basic-output-2.rb. The Linux/Unix/Mac command to do this at a command-line is "cp first.rb basic-output-2.rb" (do NOT type the quotes!). If you can't do this, you need to get help from someone on basic Linux/UNIX/Mac command-line skills.
  2. Now, use your text editor to edit the newly created program,
    basic-output-2.rb and change every instance of the word "puts" with "print". The newly edited program should look like this:

    • #!/usr/bin/ruby
      print "This is a ruby program."
      print "It is not very interesting..."
      print "Yet!"

  3. Now, run your program from lesson 1 (first.rb) and look at the result. It should look like this:

    • This is a ruby program.
      It is not very interesting...
      Yet!

  4. Now, run your new program created in this lesson (basic-output-2.rb) and look at the result. It should look like this:

    • This is a ruby program.It is not very interesting...Yet!


Summary of Lesson 2 on Basic Ruby Output:
  1. Make a copy of the first.rb program from lesson 1 and name it "basic-output-2.rb".
  2. Change the new program "basic-output-2.rb" to replace all use of the "puts" command with the "print" command. Run the changed program from the command-line and look at the output of each program.
  3. Notice the difference in the output of the two programs. In each output statement, you place what you want the statement to write to the command-line as output in quotes. In programming languages, this is usually called a string. The "puts" in Ruby puts each string of output on a separate line. The "print" command in Ruby puts each string of output on the same line. You may or may not see how this could be helpful, but trust me, it is. We will demonstrate this in our next lesson.
Now, if you understood all this you should be able to do the following exercises:
  1. Write a ruby program that uses five different lines with the "puts" command to print any five strings of your choosing on five different lines. Name your program "basic-output-2-ex1.rb". Run your program from the command-line and make sure the output looks like it should.
  2. Write a ruby program that uses five different lines with the "print" command to print any five strings of your choosing on the same line. Name your program "basic-output-2-ex2.rb". Run your program from the command-line and make sure the output looks like it should.
  3. Use the cp command to make a copy of your program from the previous step. Name the copy "basic-output-2-ex3.rb". Put two "puts" statements before all your "print" statements and put two "puts" statements after all your print statements. Run you program from the command-line. Do you see what a difference your changes made to the output?
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.