Ruby Operators Examples
Arithmetic Operators
+ Addition 2 + 2 = 4
- Subtraction 9 - 3 = 6
/ Division 8 / 4 = 2
* Multiplication 2 * 6 = 12
** Square 4 ** 2 = 16
+ = Increment a += b
- = Decrement a -= b
% Modulus 10 % 3 = 1
<< Append string << appendage
Comparison Operators
== Equal 'hello' == 'hello'
!= Not equal 4 != 3
>= Greater than or equal to 9 >= 3
> Greater than 1 > 2
<= Less than or equal to 1 <= 5
< Less than 2 < 4
! Not
<=> Combined comparison 1 <=> 2 = -1
Logical Operators
&& AND a && b
|| OR a || b
and and with lesser precedence
or or with lesser precedence
Ternary Operator
? : If condition true ? then X : otherwise Y
Assignment Operator
= Assign a = 4
Parallel Assignment
= Assign a, b = 1, 3
Range Operator
.. Inclusive range 1..10 (1,2,3,4,5,6,7,8,9,10)
... Exclusive range 1...10 (1,2,3,4,5,6,7,8,9)

Overview of + - * / operators

Understanding how to properly use operators is paramount to mastering any programming language. The first set of operators should look familiar. With the exception of modulus, the arithmetic operators are just basic math. It is important to note that Ruby understands operator precedence. Therefore, 1 + 2 * 3 is equal to 7, not 9. The multiplication of 2 * 3 is done first because it has a higher precedence. You can remember the order of operations from the acronym, PEMDAS ( Please Excuse My Dear Aunt Sally ). Or, for the order of operations (Parenthesis, Exponent, Multiplication- Division, Addition- Subtraction). In other words, whatever is inside the parenthesis should be done first, followed by any exponent, followed by multiplication and division, and finally addition and subtraction.

The modulus operator ( % ) is unique in that it may not operate as one would expect. Still, it's a useful operator that's used often across languages. It performs a division and returns the remainder. In the example above, 10 % 3 is equal to 1. With this knowledge, we can easily find if any number (x) is even by checking if x % 2 is equal to 0.

Comparison operators

It's important to distinguish between assignment and equality. The statement a = 1 is read as, "assign 'a' to the number 1". To be more technically correct, the statement should be read, "let the variable 'a' reference the number 1". As we already know, variables are proverbial boxes that contain references to objects. Of course, you'll want to come up with some type of mental shorthand, as repeating the latter example for each assignment would be a bit exhausting. Whatever your preference when reading assignment statements in your head, be sure not to use the word "equals".

Equality is denoted by the double equals ( == ) sign. The statement a == 1 is read as, "a equals 1".

Overview of incrementing / decrementing

Incrementing can be done with the shorthand += and decrementing with -=. Therefore:

>> a = 1
1
# a is equal to 1
>> b = 2
2
# b is equal to 2
>> a = a + b
3
>> a
3
# we can accomplish the same thing using +=
>> a = 1
1
>> b = 2
2
>> a += b
3
>> a
3
# a += b 
# is the same as typing 
# a = a + b

Incrementing and Decrementing are crucial to working with loops. You'll often need to "loop" over a given set of numbers, words, or objects and perform some task to each one. In the examples later in this chapter, you'll loop over a set of numbers and print out each one. In order to accomplish this, you'll need to be able to iterate over the set/collection. This is common task in every language. Ruby provides multiple options for iterating over collections, which we will explore throughout the remainder of this text.

results matching ""

    No results matching ""