Ruby Programming Language
Ruby Programming, Pseudocode and Comments
By Mark Ciotola
First published on January 25, 2019
We will develop a simple simulation as an example. We will develop it steps, so that you can develop skills as you go along. Fortunately, it is possible to develop simple simulations in Ruby without using its advanced features.
The following is a simple but complete linear simulation to model income as a function of time. The only non-intuitive aspect is the need to convert variables into strings for purposes of display (e.g. time.to s).
Before actually writing a program, it is good to create a plan and outline for the program. A plan might identify:
- The goal for the program
- The types of information that will be processed
- User input
- Program output
- The major steps in achieving the program’s goal.
First, you should answer questions about what the program should do, and why you want it to do that. Soak yourself questions such as the following:
- What goal do you want the program to achieve?
- With what input shall the user provide?
- What output should the program produce?
You can write a plan and outline for your program right in the program file by using comments. This will help save time both when writing the actual program code as well as serving as documentation. Documentation is important for when yourself or someone else needs to review the code and remember the purpose of each part.
In Ruby, code that appears after a # represents explanatory comments that do not add any functionality. For example:
# This is a comment.
The first part of the plan should answer the above questions, and identify the type of information the program will process.
Our simulation will calculate interest income. So we will need to know the initial amount of money and the interest rate. We should start our outline with a title for the program, state the goal and include our types of information. We use indenting to group together information into sections.
# INTEREST INCOME CALCULATOR # Goal: to calculate interest income. # Data and information types # Initial sum of money which is a number # Interest rate which is a number
Then we need to plan and outline exactly what the program will do. Our program needs to establish initial values for the sum and interest rate. We will call these initial values parameters. It will also need to calculate the interest income. So we add two comment lines to our outline.
# INTEREST INCOME CALCULATOR # Goal: to calculate interest income. # Data and information types # Initial sum of money which is a number # Interest rate which is a number # Initialize Parameters # Calculate and Display Results
This outline will make writing and organizing the actual code much easier.