Ruby Number Classes and Conversions
Previous | Table of Contents | Next |
Ruby Variable Scope | Ruby Methods |
Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99 |
Just about everything in Ruby is an object. Perhaps one of most surprising things is that even numbers are objects in Ruby. Most other programming languages treat numbers as primitives (which essentially means they are the building blocks from which objects are built). This means that each number type is associated with a number class and can be manipulated using the methods of that class.
Ruby Number Classes
Ruby provides a number of built-in number classes. In this section we will explore some of the more commonly used classes.
Integer Class
Fixnum Class
A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit). This effectively means that the maximum range of a Fixnum value depends on the architecture of the system on which the code is executing.
If an operation performed on a Fixnum exceeds the range defined by the system's machine word, the value is automatically converted by the interpreter to a Bignum.
Bignum Class
Bignum objects hold integers that fall outside the range of the Ruby Fixnum class. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is converted to Fixnum.
Float Class
The Float object represents real numbers based on the native architecture‘s double-precision floating point representation.
Rational Class
Rational implements a rational class for numbers.
A rational number is a number that can be expressed as a fraction p/q where p and q are integers and q is not equal to zero. A rational number p/q is said to have numerator p and denominator q. Numbers that are not rational are called irrational numbers.
Converting Numbers in Ruby
Numbers can be converted from one type to another using the Ruby Integer and Float methods. These methods take as an argument the value to be converted. For example:
Convert Floating Point Number to an Integer
Integer (10.898) => 10
Convert a String to an Integer
Integer ("10898") => 10898
Convert a Hexadecimal Number to an Integer
Integer (0xA4F5D) => 675677
Convert an Octal Number to an Integer
Integer (01231) => 665
Convert a Binary Number to an Integer
Integer (0b01110101) => 117
Convert an Character to the ASCII Character Code
For Ruby version 1.8 or earlier:
Integer (?e) => 101
For Ruby version 1.9 or later:
"e".getbyte(0) #ASCII to Integer => 101
Similarly, we can perform conversions to floating point using the Float method:
Convert an Integer Floating Point
Float (10) => 10.0
Convert a String to Floating Point
Float ("10.09889") => 10.09889
Convert a Hexadecimal Number to Floating Point
Float (0xA4F5D) => 675677.0
Convert an Octal Number to a Floating Point
Float (01231) => 665.0
Convert a Binary Number to Floating Point
Float (0b01110101) => 117
Convert a Character to a Floating Point ASCII Character Code
For Ruby version 1.8 or earlier:
Float (?e) => 101.0
For Ruby version 1.9 or later:
"e".getbyte(0) => 101.0
Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99 |
Previous | Table of Contents | Next |
Ruby Variable Scope | Ruby Methods |