Basic Output

By Mark Ciotola

First published on August 23, 2019. Last updated on February 15, 2020.

The word “puts” is used for simple output to the terminal screen. Below is some basic Ruby formatting. The “\” is called an escape character, and it tells Ruby to process the letter or symbol after that sign in a special way. For example, “\n” tells Ruby to create a new line. “\n\n” creates a blank line and then starts and content afterwards on its own line.

puts "This is a simple simulation program"
puts "\n\n" # This creates two lines

Puts outputs a string, so that every element included in a puts expression must be a string. To convert a number variable into a string, simply as “.to_s”.

To combine items of text and variables, use the “+” symbol, which means concatenate in this context. For example:

puts "Income is " + income.to_s + "."

Produces something similar to “Income is 50.” Let’s add that line to our program.

# Goal: to calculate interest income.

# Identify variable and initialize parameters
# Initial sum of money which is a number

income = 0.0 # Float

# Interest rate which is a number

time = 50 # Integer

# Calculate and Display Results

income = 10.0 ∗ time
puts "Income is " + income.to_s + "."