Programming Ruby

The Pragmatic Programmer's Guide

Previous < Contents ^
Next >

class Fixnum
Parent: Integer
Version: 1.6

Index:

Arithmetic operations Bit operations <=> [ ] id2name size to_f to_i to_s


A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit). If any operation on a Fixnum exceeds this range, the value is automatically converted to a Bignum.

Fixnum objects have immediate value. This means that when they are assigned or passed as parameters, the actual object is passed, rather than a reference to that object. Assignment does not alias Fixnum objects. There is effectively only one Fixnum object instance for any given integer value, so, for example, you cannot add a singleton method to a Fixnum.

instance methods
Arithmetic operations

Performs various arithmetic operations on fix.

fix + aNumeric Addition
fix -- aNumeric Subtraction
fix * aNumeric Multiplication
fix / aNumeric Division
fix % aNumeric Modulo
fix ** aNumeric Exponentiation

Bit operations

Performs various operations on the binary representations of the Fixnum.

~ fix Invert bits
fix | aNumeric Bitwise OR
fix & aNumeric Bitwise AND
fix ^ aNumeric Bitwise EXCLUSIVE OR
fix << aNumeric Left-shift aNumeric bits
fix >> aNumeric Right-shift aNumeric bits (with sign extension)

<=> fix <=> aNumeric -> -1, 0, +1

Comparison---Returns -1, 0, or +1 depending on whether fix is less than, equal to, or greater than aNumeric. This is the basis for the tests in Comparable.

[ ] fix[ n ] -> 0, 1

Bit Reference---Returns the nth bit in the binary representation of fix, where fix[0] is the least significant bit.

a = 0b11001100101010
30.downto(0) do |n| print a[n] end
produces:
0000000000000000011001100101010

id2name fix.id2name -> aString or nil
Returns the name of the object whose symbol id is the value of fix. If there is no symbol in the symbol table with this value, returns nil. id2name has nothing to do with the Object.id method. See also String#intern on page 371 and class Symbol on page 383.

symbol = :@inst_var » :@inst_var
id     = symbol.to_i » 9002
id.id2name » "@inst_var"

size fix.size -> aFixnum

Returns the number of bytes in the machine representation of a Fixnum.

to_f fix.to_f -> aFloat

Converts fix to a Float.

to_i fix.to_i -> fix

Returns fix.

to_s fix.to_s -> aString

Returns a string containing the decimal representation of self.


Previous < Contents ^
Next >

Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/)).

Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.

Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.