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.

No comments:

Post a Comment