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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.