Ruby: Is And Isn't It True Or False
Arithmetic and Comparison Operators
In Ruby, an expression or statement is made up of values and operators.
Take a look at the equation 4 + 6 == 10
. Within this equation there is an arithmetic operator (+
, -
, *
, /
and %
) and there is a comparison operator (==
, !=
, >
, <
, >=
, <=
, <=>
, ===
). The above equation of 4 + 6 == 10
has a boolean value of true
, because the expressions on either side of the ==
operator evaluate the same amount, as required by ==
. If we used a different comparison operator, and changed the equation to 4 + 6 < 10
, the equation now has a boolean value of false
.
These operators are very useful when we want a programme to perform certain actions based upon whether a condition is true or false. Lets take a look at what each of these operators mean:
arithmetic operator | means: |
+ | adds |
- | subtracts |
* | multiplies |
/ | divides |
% | modulus, this returns a value that is the remainder when one number is divided by another |
Comparison Operator | Means: |
== | Is equal to |
!= | Is not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Here we can see that the !
acts as a negating character, and basically says ‘is not’ to the operator that it sits with. For instance !>
would be the same as putting <
.
Logical and Boolean Operators
Similarly !
, or ‘is not’, can be used with booleans to create expressions such as false == !true
. Other boolean operators are and
: &&
, as well as or
: ||
. In an &&
expression, both the right and left side must be true
for the expression to be true
. In an ||
expression, only one side must be true
for the expression to be true
.
AND | |
True && True | True |
True && False | False |
False && True | False |
False && False | False |
OR | |
True || True | True |
True || False | True |
False || True | True |
False || False | False |
Short Circuit Evaluation
Because Ruby operates with this logic if an &&
equation is being evaluated and the first expression within it is false
, then Ruby knows that the answer must be false, because an &&
equation needs both sides to be true
for the evaluation to be true
. This is called short-circuit evaluation. In this case the second half of the equation is not evaluated to reach a conclusion.
The same goes for ||
statements, where if the first part of the equation is true
, Ruby knows that the evaluation must be true
. The second part of the equation is not evaluated, the evaluation is ‘short-circuited’.
The Combined Comparison Operator
The combined comparison operator, or <=>
, compares both sides or an equation and returns a value. If the first operand is greater than the second, the equation will evaluate to 1. If the first operand is equal to the second operand, the equation will evaluate to 0. If the first operand is lesser than the second operand, the equation is evaluate to -1. For instance:
3 <=> 1
will evaluate to 1
3 <=> 3
will evaluate to 0
1 <=> 3
will evaluate to -1