Numbers in Ruby


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 program p002rubynumbers.rb

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 Complex, BigDecimal, 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:

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:

The output is:

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:

or, more idiomatically, as:

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:

This expression simply would not work as intended, if written with && instead of and.

Note: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various Ruby Gurus), my acknowledgment and thanks to all of them. Much of the material on rubylearning.com and in the course at rubylearning.org is drawn primarily from the Programming Ruby book, available from The Pragmatic Bookshelf.