Operators

Many control structures, as well as other kinds of statements, rely upon various operators. Chapters 3 and 4 introduced some of these, including the assignment, arithmetic, array, and hash operators. Table 5.1 adds to those the comparison and logical operators. These operators can be used in a statement that will return a Boolean value:

puts 2 > 3 # falseputs 3 > 2 && 3 < 4 # true

Table 5.1

image

Most of the operators listed in Table 5.1 are easy to understand, but I do want to highlight the equality operator, which is two equals signs together. This is different from a single equals sign, which is the assignment operator:

x = 2 # Assignment ...

Get Ruby: Visual Quickstart Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.