Difference between revisions of "Ruby Number Classes and Conversions"
(New page: 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 (wh...) |
(No difference)
|
Revision as of 21:03, 15 November 2007
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 theyt are the building blocks from which objects are built). This means that each number type is associated with a number class, along with the various methods associated with that class.
Ruby Number Classes
Ruby provides a number of builtin number classes. In this section we will explore some of the more commonly used classes.
Integer Class
The base class from which the following number classes are derived.
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 range defined by the system's marchine 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 != 0. 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 type to another using the Ruby Integer and Float methods. This methods takes 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 (01110101) => 299073
Convert an Character to the ASCII Character Code
Integer (?e) => 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 (01110101) => 299073.0
Convert an Character to a Floating Point ASCII Character Code
Float (?e) => 101.0