Ruby Programming Language

Basic Output


First published on January 30, 2019

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.

[code lang=”ruby”]
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:

[code lang=”ruby”]
puts  ”Income is ” + income.to_s + ”.”

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

[code lang=”ruby”]
# 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 = 0  # Integer

# Calculate and Display Results

income = 10.0 ∗ time

puts  ”Income is ” + income.to_s + ”.”



Content is copyright the author. Layout is copyright Corsbook. See for further notices.