Ruby 2 Numbers

Let's play with Numbers. In Ruby, numbers without decimal points are called integers, and numbers with decimal points are usually called floating-point numbers or, more simply, floats (you must place at least one digit before the decimal point). An integer literal is simply a sequence of digits eg. 0, 123, 123456789. Underscores may be inserted into integer literals (though not at the beginning or end), and this feature is sometimes used as a thousands separator eg. 1_000_000_000. Underscore characters are ignored in the digit string. Here's programp002rubynumbers.rb
  1. # p002rubynumbers.rb  
  2. =begin  
  3.  Ruby Numbers  
  4.  Usual operators:  
  5.  + addition  
  6.  - subtraction  
  7.  * multiplication  
  8.  / division  
  9. =end  
  10.   
  11. puts 1 + 2  
  12. puts 2 * 3  
  13. # Integer division  
  14. # When you do arithmetic with integers, you'll get integer answers  
  15. puts 3 / 2  
  16. puts 10 - 11  
  17. puts 1.5 / 2.6  
Ruby integers are objects of class Fixnum or Bignum. The Fixnum and Bignum classes represent integers of differing sizes. Both classes descend from Integer (and therefore Numeric). The floating-point numbers are objects of class Float, corresponding to the native architecture's double data type. The ComplexBigDecimal, and Rational classes are not built-in to Ruby but are distributed with Ruby as part of the standard library. We shall be talking about classes in detail later.
A part of the class hierarchy is as shown in the figure below:
Class Hierarchy

Operators and Precedence

Let us look at Ruby's operators (courtesy: Dave Thomas' - Programming Ruby). They are arranged here in order from highest to lowest precedence.
Operators in Ruby
a. The increment and decrement operators (++ and --) are not available in Ruby, neither in "pre" nor "post" forms. However, do note that the += and -= are available.
b. Brackets (parentheses) work the same way as with regular arithmetic. Anything inside brackets is calculated first (or, more technically, given higher precedence).
c. The check marked operators is a kind of syntactic sugar (more on this later) - where something looks like an operator but is a method call.
The Ruby modulus operator's (%) behavior is as follows:
  1. puts (5 % 3)     # prints  2  
  2. puts (-5 % 3)    # prints  1  
  3. puts (5 % -3)    # prints -1  
  4. puts (-5 % -3)   # prints -2  
Ruby's definition of the modulo (%) operator differs from that of C and Java. 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.

Difference between or and || operator.

Both or and || return their first argument unless it is false, in which case they evaluate and return their second argument. This is shown in the following example:
  1. puts nil || 2008  
  2. puts false || 2008  
  3. puts "ruby" || 2008  
The output is:
  1. >ruby test.rb  
  2. 2008  
  3. 2008  
  4. ruby  
  5. >Exit code: 0  
The only difference between or and || is their precedence. || has a higher precedence than or.

A common idiom is to use || to assign a value to a variable only if that variable isn't already set. This can be written as:
  1. @variable = @variable || "default value"  
or, more idiomatically, as:
  1. @variable ||= "default value"  
One reason for these alternate versions of the Boolean operators is the fact that they have lower precedence than the assignment operator. This means that you can write a Boolean expression such as the following that assigns values to variables until it encounters a false value:
  1. if a = f(x) and b = f(y) and c = f(z) then d = g(a,b,c) end  
This expression simply would not work as intended, if written with && instead of and.
  1. def g *args # The splat here says accept 1 or more arguments, in the form of an Array  
  2.   args      # This returns an array  
  3. end  
  4.   
  5. def f arg  
  6.   arg  
  7. end  
  8.   
  9. x,y,z = [true'two'false# parrallel assignment lets us do this  
  10.   
  11. if a = f(x) and b = f(y) and c = f(z) then  
  12.   d = g(a,b,c) # An array is returned, and stored in variable d  
  13. end  
  14.   
  15. p d # using p to puts and inspect d