Class Fixnum
In: numeric.c
lib/rational.rb
Parent: Integer

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.

Methods

%   &   *   **   **   +   -   -@   /   <   <<   <=   <=>   ==   >   >=   >>   []   ^   abs   div   divmod   id2name   induced_from   modulo   power!   quo   quo   rdiv   rpower   size   to_f   to_s   to_sym   zero?   |   ~  

Included Modules

Precision

Public Class methods

Convert obj to a Fixnum. Works with numeric parameters. Also works with Symbols, but this is deprecated.

Public Instance methods

Returns fix modulo other. See Numeric.divmod for more information.

Performs multiplication: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

**(other)

Alias for rpower

Raises fix to the other power, which may be negative or fractional.

  2 ** 3      #=> 8
  2 ** -1     #=> 0.5
  2 ** 0.5    #=> 1.4142135623731

Performs addition: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

Performs subtraction: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

Negates fix (which might return a Bignum).

Performs division: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

Returns true if the value of fix is less than that of other.

Shifts fix left count positions (right if count is negative).

Returns true if the value of fix is less thanor equal to that of other.

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

Return true if fix equals other numerically.

  1 == 2      #=> false
  1 == 1.0    #=> true

Returns true if the value of fix is greater than that of other.

Returns true if the value of fix is greater than or equal to that of other.

Shifts fix right count positions (left if count is negative).

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

Bitwise EXCLUSIVE OR.

Returns the absolute value of fix.

   -12345.abs   #=> 12345
   12345.abs    #=> 12345

Performs division: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

Returns the name of the object whose symbol id is 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 Fixnum#to_sym, String#intern, and class Symbol.

   symbol = :@inst_var    #=> :@inst_var
   id     = symbol.to_i   #=> 9818
   id.id2name             #=> "@inst_var"

Returns fix modulo other. See Numeric.divmod for more information.

power!(p1)

Alias for #**

Returns the floating point result of dividing fix by numeric.

   654321.quo(13731)      #=> 47.6528293642124
   654321.quo(13731.24)   #=> 47.6519964693647

If Rational is defined, returns a Rational number instead of a Fixnum.

rdiv(p1)

Alias for quo

Returns a Rational number if the result is in fact rational (i.e. other < 0).

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

   1.size            #=> 4
   -1.size           #=> 4
   2147483647.size   #=> 4

Converts fix to a Float.

Returns a string containing the representation of fix radix base (between 2 and 36).

   12345.to_s       #=> "12345"
   12345.to_s(2)    #=> "11000000111001"
   12345.to_s(8)    #=> "30071"
   12345.to_s(10)   #=> "12345"
   12345.to_s(16)   #=> "3039"
   12345.to_s(36)   #=> "9ix"

Returns the symbol whose integer value is fix. See also Fixnum#id2name.

   fred = :fred.to_i
   fred.id2name   #=> "fred"
   fred.to_sym    #=> :fred

Returns true if fix is zero.

One’s complement: returns a number where each bit is flipped.

To view or add comments on this documentation, please go to the API wiki.

[Validate]