Comparison Operators - Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)

Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)

Comparison Operators

A basic understanding of algebra is all you need for this book. If you're new to programming, you might be relieved to know that you don't need to be a math genius to be a successful developer. It's called a programming language for a reason.

Below are the comparison operators you need to know in Ruby.

== Is equal to

!= Is not equal to

> Is greater than

< Is less than

>= Is greater than or equal to

<= Is less than or equal to

Booleans

Comparisons are used to check whether something is true or false. What is returned is a boolean. A boolean is another ruby data type. It returns a true or false statement.

Take a look at these booleans:

9 == 9 returns true

9 != 9 returns false

6 > 4 returns true

12 < 10 returns false

50 >= 34 returns true

25 <= 20 returns false

Note: When a statement is true, Ruby will return true. However, under the hood Ruby is returning a 1 when true and a 0 when false. This will make sense soon.

We aren't limited to numbers when testing.

"tree" == "tree" returns true

"leaf" == "branch" returns false

"ground" != "sky" returns true

You can also set variables as booleans themselves.

is_eligible = true

Combined Comparison Operator

The Combined Comparison Operator is also known as the Spaceship Operator. Graphically it looks like an alien spacecraft. Beaming it to you now:

3 <=> 5 returns -1

3 <=> 3 returns 0

3 <=> 2 returns 1

What's happening here?

The item on the left is compared to the item on the right. When the item on the left is less than the item on the right, -1 is returned. When the item on the left is equal to the item on the right, 0 is returned. When the item on the left is greater than the item on the right, 1 is returned.

Right < Left returns -1

Right == Left returns 0

Right > Left returns 1

Using this logic, can you figure out what these will return:

54 <=> 21

65 <=> 98

12 <=> 12

Boolean Operators

We can use more than comparative operators in Ruby. We can also use what's known as boolean or logical operators. Here they are:

&& and

|| or

! not

For && you need the booleans on both the right and the left to be true. Here's some sample results:

true && true returns true

false && true returns false

true && false returns false

false && false returns false

3 > 2 && 5 < 8 returns true

For || you need either the right or the left to be true.

true || true returns true

false || true returns true

true || false returns true

false || false returns false

6 == 6 || 4 > 8 returns true

The ! will convert true values to false and false values to true.

!true returns false

!false returns true

Comments on Comments

"This is madness!" -Persian Messenger

"Madness. This is Sparta!" - Leonidas

This exchange from the movie, "300" is a great example of why communication is important. If you've seen the movie you know that Leonidas kicks the messenger down into a seemingly bottomless sinkhole after correcting him. We need to make sure to add comments to our code. That way everyone in the future will know what "this" is.

Comments are lines in the code that Ruby ignores. They are there for the programmer to read. Here's how both single and multi-line comments are expressed:

=begin

This is a multi-line comment.

It can be as long as you need it to be.

=end

Make sure =begin and =end are on their own lines.

Single comment lines need only start with a hashtag.

# This is a single line comment.