Class Fixnum
In: numeric.c
lib/mathn.rb
lib/rational.rb
lib/rexml/xpath_parser.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   dclone   div   divmod   even?   fdiv   induced_from   modulo   odd?   power!   quof   rpower   size   succ   to_f   to_s   zero?   |   ~  

Included Modules

Precision

External Aliases

quo -> /
quo -> rdiv

Public Class methods

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

Public Instance methods

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

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 than or 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 integer division: returns integer value.

Returns true if fix is an even number.

Returns the floating point result of dividing fix by numeric.

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

Returns true if fix is an odd number.

power!(p1)

Alias for #**

quof(p1)

Alias for fdiv

rpower(p1)

Alias for #**

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

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

Returns the Integer equal to int + 1.

   1.next      #=> 2
   (-1).next   #=> 0

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 true if fix is zero.

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

[Validate]

ruby-doc.org is a service of James Britt and Happy Camper Studios, a Ruby application development company in Phoenix, AZ.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.