Difference between revisions of "Ruby Operator Precedence"
(New page: In the previous chapter of Ruby Essentials we looked at Ruby operators and expressions. An equally important area or understand is operator precedence. This is essentially the order in...) |
(→Operator Precedence Table) |
||
Line 45: | Line 45: | ||
<td>Element reference, element set</td> | <td>Element reference, element set</td> | ||
</tr><tr> | </tr><tr> | ||
− | <td> | + | <td>Yes</td> |
<td><code>**</code></td> | <td><code>**</code></td> | ||
<td>Exponentiation (raise to the power)</td> | <td>Exponentiation (raise to the power)</td> | ||
</tr><tr> | </tr><tr> | ||
− | <td> | + | <td>Yes</td> |
<td><code>!</code> <code>~</code> <code>+</code> <code>-</code></td> | <td><code>!</code> <code>~</code> <code>+</code> <code>-</code></td> | ||
<td>Not, complement, unary plus and minus (method names for the last two are <code>+@</code> and <code>-@</code>)</td> | <td>Not, complement, unary plus and minus (method names for the last two are <code>+@</code> and <code>-@</code>)</td> | ||
</tr><tr> | </tr><tr> | ||
− | <td> | + | <td>Yes</td> |
<td><code>*</code> <code>/</code> <code>%</code></td> | <td><code>*</code> <code>/</code> <code>%</code></td> | ||
<td>Multiply, divide, and modulo</td> | <td>Multiply, divide, and modulo</td> | ||
</tr><tr> | </tr><tr> | ||
− | <td> | + | <td>Yes</td> |
<td><code>+</code> <code>-</code></td> | <td><code>+</code> <code>-</code></td> | ||
<td>Addition and subtraction</td> | <td>Addition and subtraction</td> | ||
</tr><tr> | </tr><tr> | ||
− | <td> | + | <td>Yes</td> |
<td><code>>></code> <code><<</code></td> | <td><code>>></code> <code><<</code></td> | ||
<td>Right and left bitwise shift</td> | <td>Right and left bitwise shift</td> | ||
</tr><tr> | </tr><tr> | ||
− | <td> | + | <td>Yes</td> |
<td><code>&</code></td> | <td><code>&</code></td> | ||
<td>Bitwise `AND'</td> | <td>Bitwise `AND'</td> | ||
</tr><tr> | </tr><tr> | ||
− | <td> | + | <td>Yes</td> |
<td><code>^</code> <code>|</code></td> | <td><code>^</code> <code>|</code></td> | ||
<td>Bitwise exclusive `OR' and regular `OR'</td> | <td>Bitwise exclusive `OR' and regular `OR'</td> | ||
</tr><tr> | </tr><tr> | ||
− | <td> | + | <td>Yes</td> |
<td><code><=</code> <code><</code> <code>></code> <code>>=</code></td> | <td><code><=</code> <code><</code> <code>></code> <code>>=</code></td> | ||
<td>Comparison operators</td> | <td>Comparison operators</td> | ||
</tr><tr> | </tr><tr> | ||
− | <td> | + | <td>Yes</td> |
<td><code><=></code> <code>==</code> <code>===</code> <code>!=</code> <code>=~</code> <code>!~</code></td> | <td><code><=></code> <code>==</code> <code>===</code> <code>!=</code> <code>=~</code> <code>!~</code></td> | ||
Revision as of 18:29, 19 November 2007
In the previous chapter of Ruby Essentials we looked at Ruby operators and expressions. An equally important area or understand is operator precedence. This is essentially the order in which the Ruby interpreter evaluates expressions comprising more than one operator.
An Example of Ruby Operator Precedence
When humans evaluate expressions, they usually do so starting at the left of the expression and working towards the right. For example, working from left to right we get a result of 300 from the following expression:
10 + 20 * 10 = 300
This is because we, as humans, add 10 to 20, resulting in 30 and then multiply that by 10 to arrive at 300. Ask Ruby to perform the same calculation and you get a very different answer:
irb(main):003:0> 10 + 20 * 10 => 210
This is a direct result of operator precedence. Ruby has a set of rules that tell it in which order operators should be evaluated in an expression. Clearly, Ruby considers the multiplication operator (*) to be of a higher precedence than the addition (+) operator.
Overriding Operator Precedence
The precedence built into Ruby can be overided by surrounding the lower priority section of an expression is parentheses. For example:
(10 + 20) * 10 => 300
In the above example, the expression fragment enclosed in parentheses is evaluated before the hight precedence multiplications.
Operator Precedence Table
The following table provides a reference when you need to to know the operator precedence used by Ruby. The table lists all operators from highest precendence to lowest.
Method | Operator | Description |
---|---|---|
Y | [ ] [ ]= |
Element reference, element set |
Yes | ** |
Exponentiation (raise to the power) |
Yes | ! ~ + - |
Not, complement, unary plus and minus (method names for the last two are +@ and -@ ) |
Yes | * / % |
Multiply, divide, and modulo |
Yes | + - |
Addition and subtraction |
Yes | >> << |
Right and left bitwise shift |
Yes | & |
Bitwise `AND' |
Yes | ^ | |
Bitwise exclusive `OR' and regular `OR' |
Yes | <= < > >= |
Comparison operators |
Yes | <=> == === != =~ !~ |
Equality and pattern match operators (!= and !~ may not be defined as methods) |
&& |
Logical `AND' | |
|| |
Logical `AND' | |
.. ... |
Range (inclusive and exclusive) | |
? : |
Ternary if-then-else | |
= %= { /= -= += |= &= >>= <<= *= &&= ||= **= |
Assignment | |
defined? |
Check if specified symbol defined | |
not |
Logical negation | |
or and |
Logical composition | |
if unless while until |
Expression modifiers | |
begin/end |
Block expression |