Displaying text in Ruby is simple. Try entering the following at the command prompt:
irb
(
main
):
001
:
0
>
puts
"Hello World"
The puts
method (short for
outPUT a String) simply displays a string on the screen. Notice the
string is contained in quotation marks; otherwise, both Hello
and World
would be interpreted as variables.
Variables are displayed on the screen using similar syntax, except
without quotation marks:
irb
(
main
):
002
:
0
>
text
=
"Hello World"
=>
"Hello World"
irb
(
main
):
003
:
0
>
puts
text
=>
Hello
World
This example stores the string “Hello
World”
in a variable named text
and then displays the value stored in the
text
variable using the puts
method. This method is not limited to
strings and can be used with other classes including integers, floats,
and Booleans.
The use of classes to define data types means a variety of methods
can be done for each type. For example, x.length
indicates the size of a string when
x
is defined as a string.
Displaying user input in Ruby is almost as easy as displaying output. Try entering the following in the command prompt:
irb
(
main
):
001
:
0
>
age_input
=
gets
The gets
method (short for GET
a String from the user) stops the program and waits for the user to type
some text and then press Enter. The text typed by the user will be
stored as a string in a variable called age_input
. Due to the nature of the gets
method, the value stored in age_input
will be a string, but you need an
integer if you wish to mathematically manipulate it. We create another
variable age
and set it equal to the
integer value of the user’s input by converting the string age_input
to an integer. This is done by
issuing the following command:
irb
(
main
):
002
:
0
>
age
=
age_input
.
to_i
The method to_i
converts
age_input
to an integer.
Now that you have learned how to display text and request user input, you can develop a program that calculates the area of a rectangle. Try using the problem-solving approach discussed in Chapter 1 to create this program.
Ask yourself key questions that must be answered to properly design the program:
How do you find the area of a rectangle?
How many variables do you need to represent that area?
What data type do the variables need to be?
Before writing out the problem, remember that the input method stores user input as strings, so we need to convert the lengths (which are stored as strings) to integers before performing mathematical operations with them.
Ask for the length.
Store the length.
Ask for the width.
Store the width.
Convert the length to an integer.
Convert the width to an integer.
Calculate rectangle area (area = length × width).
Display area.
The equation for the area of a rectangle is the product of its length and width. Although it does not affect this equation, remember that the rules for order of operations apply in Ruby. To change the order of operations, use parentheses.
See Example 3-1. In the figure, as with
all other program illustrations, the line numbers are not part of the
code; they are added strictly for explanatory purposes. In line 8, we
are printing an integer represented in the variable area
. This differs from the printing of
character strings in lines 1 and 3. Ruby automatically determines the
data type, if it can. This is called dynamic or “duck” typing. This
functionality is helpful; however, as you will see, automatic type
determination can introduce problems. In this example, though, it helps
in that puts area
is mapped to
puts area.to_s
. Note that generally
speaking, the .to_*
method is a type
conversion operation where the target type is represented by the
*
(a wildcard). For example, you have
now seen conversion to integer (to_i
)
and to string (to_s
).
1
puts
"What is the length?"
# Ask for the length
2
length_input
=
gets
# Stores the length
3
puts
"What is the width?"
# Ask for the width
4
width_input
=
gets
# Stores the width
5
length
=
length_input
.
to_i
# Convert length to integer
6
width
=
width_input
.
to_i
# Convert width to integer
7
area
=
length
*
width
# Calculate rectangle area
8
puts
area
# Display area
Similarly those who are adventurous can type the code lines from step 3 into a file and run the code from there. For those who are unsure, no worries; we address files later on. For now, simply see if you get the desired results; if not, make sure you typed everything correctly.
If it works, congratulations! You have just created a program that calculates the area of a rectangle.
18.189.195.34