Ruby Operators
Table of Contents

Modulus

   1. puts (5 % 3)     # prints  2  
   2. puts (-5 % 3)    # prints  1  
   3. puts (5 % -3)    # prints -1  
   4. puts (-5 % -3)   # prints -2

In Ruby, -7%3 is 2. In C and Java, the result is -1 instead. In Ruby, the sign of the result (for % operator) is always the same as the sign of the second operand.

OR

Both or and || return their first argument unless it is false, in which case they evaluate and return their second argument. The only difference between or and || is their precedence. || has a higher precedence than or.

puts nil || 2008  # prints 2008
puts false || 2008  # prints false
puts "ruby" || 2008 # prints ruby
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.