Showing posts with label operator. Show all posts
Showing posts with label operator. Show all posts

Friday, October 14, 2011

Comparison of operators with c or other languages

  • Many of Ruby's operators are similar or identical to those in C. Two notable exceptions are the increment and decrement operators (++ and --). These are not available in Ruby, neither in "pre" nor "post" forms.
  • The modulus operator is known to work somewhat differently in different languages with respect to negative numbers. The two sides of this argument are beyond the scope of this book; Ruby's behavior is as follows:
    puts (5 % 3)    # Prints 2
    puts (-5 % 3) # Prints 1
    puts (5 % -3) # Prints -1
    puts (-5 % -3) # Prints -2
  • Most of Ruby's operators are really methods; the "punctuation" form of these methods is provided for familiarity and convenience. The first exception is the set of reflexive assignment operators (+=, -=, *=, and so on); the second exception is the following set: = .. ... ! not && and || or != !~.
  • Like most (though not all) modern languages, Boolean operations are always short-circuited; that is, the evaluation of a Boolean expression stops as soon as its truth value is known. In a sequence of or operations, the first true will stop evaluation; in a string of and operations, the first false will stop evaluation.
  • The simpler Fixnum type is passed as an immediate value and thus may not be changed from within methods. The same is true for true, false, and nil.
  • Do not confuse the && and || operators with the & and | operators. These are used as in C; the former are for Boolean operations, and the latter are for arithmetic or bitwise operations.
  • The and-or operators have lower precedence than the &&-|| operators. See the following code fragment:
    a = true
    b = false
    c = true
    d = true
    a1 = a && b or c && d # &&'s are done first
    a2 = a && (b or c) && d # or is done first
    puts a1 # Prints false
    puts a2 # Prints true
  • Additionally, be aware that the assignment "operator" has a higher precedence than the and and or operators! (This is also true for the reflexive assignment operators +=, -=, and the others.) For example, in the following code x = y or z looks like a normal assignment statement, but it is really a freestanding expression (equivalent to (x=y) or z, in fact). The third section shows a real assignment statement x = (y or z), which may be what the programmer really intended.
    y = false
    z = true

    x = y or z # = is done BEFORE or!
    puts x # Prints false

    (x = y) or z # Line 5: Same as previous
    puts x # Prints false

    x = (y or z) # or is done first
    puts x # Prints true
  • Some operators can't be overloaded because they are built into the language rather than implemented as methods. These are: = .. ... and or not && || ! != !~. Additionally, the reflexive assignment operators (+=, -=, and so on) cannot be overloaded. These are not methods, and it can be argued they are not true operators either.
  • There is never any assignment with the scope operator; for example, the assignment Math::PI = 3.2 is illegal.

Tuesday, October 11, 2011

Operators and precedence in Ruby

Now that we have established our most common data types, let's look at Ruby's operators. They are arranged here in order from highest to lowest precedence:

::
Scope
[]
Indexing
**
Exponentiation
+ - ! ~
Unary pos/neg, not, ....
* / %
Multiplication, division, ....
+ -
Addition/subtraction
<< >>
Logical shifts, ...
&
Bitwise and
| ^
Bitwise or, xor
> >= < <=
Comparison
== === <=> != =~ !~
Equality, inequality, ....
&&
Boolean and
||
Boolean or
.. ...
Range operators
= (also +=, -=, ...)
Assignment
?:
Ternary decision
not
Boolean negation
and or
Boolean and, or


Some of the preceding symbols serve more than one purpose; for example, the operator << is a bitwise left shift but is also an append operator (for arrays, strings, and so on) and a marker for a here-document. Likewise the + is for numeric addition as well as for string concatenation. As we shall see later, many of these operators are just shortcuts for method names.