Changes

Jump to: navigation, search

Ruby Operator Precedence

4,406 bytes added, 18:28, 19 November 2007
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...
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:

<pre>
irb(main):003:0> 10 + 20 * 10
=> 210
</pre>

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:

<pre>
(10 + 20) * 10
=> 300
</pre>

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.

<table border="1" cellspacing="0">
<tr style="background:#efefef;">
<caption>Ruby operators (highest to lowest precedence)</caption>
<tr style="background:#efefef;">
<th>Method</th>
<th>Operator</th>
<th>Description</th>
</tr>

<tr class="firstRow">
<td>Y</td>
<td><code>[ ]</code> <code>[ ]=</code></td>
<td>Element reference, element set</td>
</tr><tr>
<td>Y</td>

<td><code>**</code></td>
<td>Exponentiation (raise to the power)</td>
</tr><tr>
<td>Y</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>
</tr><tr>
<td>Y</td>
<td><code>*</code> <code>/</code> <code>%</code></td>

<td>Multiply, divide, and modulo</td>
</tr><tr>
<td>Y</td>
<td><code>+</code> <code>-</code></td>
<td>Addition and subtraction</td>
</tr><tr>

<td>Y</td>
<td><code>&gt;&gt;</code> <code>&lt;&lt;</code></td>
<td>Right and left bitwise shift</td>
</tr><tr>
<td>Y</td>
<td><code>&amp;</code></td>
<td>Bitwise `AND'</td>

</tr><tr>
<td>Y</td>
<td><code>^</code> <code>|</code></td>
<td>Bitwise exclusive `OR' and regular `OR'</td>
</tr><tr>
<td>Y</td>

<td><code>&lt;=</code> <code>&lt;</code> <code>&gt;</code> <code>&gt;=</code></td>
<td>Comparison operators</td>
</tr><tr>
<td>Y</td>
<td><code>&lt;=&gt;</code> <code>==</code> <code>===</code> <code>!=</code> <code>=~</code> <code>!~</code></td>

<td>Equality and pattern match operators (<code>!=</code> and <code>!~</code> may not be defined as methods)</td>
</tr><tr>
<td></td>
<td><code>&amp;&amp;</code></td>
<td>Logical `AND'</td>

</tr><tr>
<td></td>
<td><code>||</code></td>
<td>Logical `AND'</td>
</tr><tr>
<td></td>
<td><code>..</code> <code>...</code></td>

<td>Range (inclusive and exclusive)</td>
</tr><tr>
<td></td>
<td><code>? :</code></td>
<td>Ternary if-then-else</td>
</tr><tr>
<td></td>

<td><code>=</code> <code>%=</code> <code></code>{ <code>/=</code> <code>-=</code> <code>+=</code> <code>|=</code> <code>&amp;=</code> <code>&gt;&gt;=</code> <code>&lt;&lt;=</code> <code>*=</code> <code>&amp;&amp;=</code> <code>||=</code> <code>**=</code></td>

<td>Assignment</td>
</tr><tr>
<td></td>
<td><code>defined?</code></td>
<td>Check if specified symbol defined</td>
</tr><tr>
<td></td>

<td><code>not</code></td>
<td>Logical negation</td>
</tr><tr>
<td></td>
<td><code>or</code> <code>and</code></td>
<td>Logical composition</td>

</tr><tr>
<td></td>
<td><code>if</code> <code>unless</code> <code>while</code> <code>until</code></td>
<td>Expression modifiers</td>
</tr><tr>

<td></td>
<td><code>begin/end</code></td>
<td>Block expression</td>
</tr>
</table>

Navigation menu