Changes

Jump to: navigation, search

Ruby Number Classes and Conversions

3,118 bytes added, 21:03, 15 November 2007
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...
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 ===

<pre>
Integer (10.898)
=> 10
</pre>

=== Convert a String to an Integer ===

<pre>
Integer ("10898")
=> 10898
</pre>

=== Convert a Hexadecimal Number to an Integer ===

<pre>
Integer (0xA4F5D)
=> 675677
</pre>

=== Convert an Octal Number to an Integer ===

<pre>
Integer (01231)
=> 665
</pre>

=== Convert a Binary Number to an Integer ===

<pre>
Integer (01110101)
=> 299073
</pre>

=== Convert an Character to the ASCII Character Code ===

<pre>
Integer (?e)
=> 101
</pre>

Similarly, we can perform conversions to floating point using the Float method:


=== Convert an Integer Floating Point ===

<pre>
Float (10)
=> 10.0
</pre>

=== Convert a String to Floating Point ===

<pre>
Float ("10.09889")
=> 10.09889
</pre>

=== Convert a Hexadecimal Number to Floating Point ===

<pre>
Float (0xA4F5D)
=> 675677.0
</pre>

=== Convert an Octal Number to a Floating Point ===

<pre>
Float (01231)
=> 665.0
</pre>

=== Convert a Binary Number to Floating Point ===

<pre>
Float (01110101)
=> 299073.0

</pre>

=== Convert an Character to a Floating Point ASCII Character Code ===

<pre>
Float (?e)
=> 101.0
</pre>

Navigation menu