RubyLearning

Helping Ruby Programmers become Awesome!

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

# p002rubynumbers.rb
=begin
 Ruby Numbers
 Usual operators:
 + addition
 - subtraction
 * multiplication
 / division
=end

puts 1 + 2
puts 2 * 3
# Integer division
# When you do arithmetic with integers, you'll get integer answers
puts 3 / 2
puts 10 - 11
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 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:

puts (5 % 3)     # prints  2
puts (-5 % 3)    # prints  1
puts (5 % -3)    # prints -1
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:

puts nil || 2008
puts false || 2008
puts "ruby" || 2008

The output is:

>ruby test.rb
2008
2008
ruby
>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:

@variable = @variable || "default value"

or, more idiomatically, as:

@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:

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.

def g *args # The splat here says accept 1 or more arguments, in the form of an Array
  args      # This returns an array
end

def f arg
  arg
end

x,y,z = [true, 'two', false] # parrallel assignment lets us do this

if a = f(x) and b = f(y) and c = f(z) then
  d = g(a,b,c) # An array is returned, and stored in variable d
end

p d # using p to puts and inspect d

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.