Class: Fixnum

Inherits:
Integer show all
Includes:
Precision
Defined in:
numeric.c,
numeric.c

Overview

******************************************************************

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

<code>Fixnum</code> 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 <code>Fixnum</code> objects. There is effectively only one
<code>Fixnum</code> object instance for any given integer value, so,
for example, you cannot add a singleton method to a
<code>Fixnum</code>.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Precision

included, #prec, #prec_f, #prec_i

Methods inherited from Integer

#ceil, #chr, #downto, #floor, #integer?, #next, #round, #succ, #times, #to_i, #to_int, #truncate, #upto

Methods inherited from Numeric

#+@, #ceil, #coerce, #eql?, #floor, #initialize_copy, #integer?, #nonzero?, #remainder, #round, #singleton_method_added, #step, #to_int, #truncate

Methods included from Comparable

#between?

Class Method Details

.induced_from(obj) ⇒ Fixnum

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

Returns:



1836
1837
1838
# File 'numeric.c', line 1836

static VALUE
rb_fix_induced_from(klass, x)
VALUE klass, x;

Instance Method Details

#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric

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

Overloads:



2147
2148
2149
# File 'numeric.c', line 2147

static VALUE
fix_mod(x, y)
VALUE x, y;

#&(other) ⇒ Integer

Bitwise AND.

Returns:



2375
2376
2377
# File 'numeric.c', line 2375

static VALUE
fix_and(x, y)
VALUE x, y;

#*(numeric) ⇒ Object

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



2034
2035
2036
# File 'numeric.c', line 2034

static VALUE
fix_mul(x, y)
VALUE x, y;

#**(other) ⇒ Numeric

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

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

Returns:



2192
2193
2194
# File 'numeric.c', line 2192

static VALUE
fix_pow(x, y)
VALUE x, y;

#+(numeric) ⇒ Object

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



1974
1975
1976
# File 'numeric.c', line 1974

static VALUE
fix_plus(x, y)
VALUE x, y;

#-(numeric) ⇒ Object

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



2004
2005
2006
# File 'numeric.c', line 2004

static VALUE
fix_minus(x, y)
VALUE x, y;

#-Integer

Negates fix (which might return a Bignum).

Returns:



1896
1897
1898
# File 'numeric.c', line 1896

static VALUE
fix_uminus(num)
VALUE num;

#/(numeric) ⇒ Object #div(numeric) ⇒ Object

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



2125
2126
2127
# File 'numeric.c', line 2125

static VALUE
fix_div(x, y)
VALUE x, y;

#<(other) ⇒ Boolean

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

Returns:

  • (Boolean)


2313
2314
2315
# File 'numeric.c', line 2313

static VALUE
fix_lt(x, y)
VALUE x, y;

#<<(count) ⇒ Integer

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

Returns:



2437
2438
2439
# File 'numeric.c', line 2437

static VALUE
fix_lshift(x, y)
VALUE x, y;

#<=(other) ⇒ Boolean

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

Returns:

  • (Boolean)


2336
2337
2338
# File 'numeric.c', line 2336

static VALUE
fix_le(x, y)
VALUE x, y;

#<=>(numeric) ⇒ -1, ...

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.

Returns:

  • (-1, 0, +1)


2243
2244
2245
# File 'numeric.c', line 2243

static VALUE
fix_cmp(x, y)
VALUE x, y;

#==(other) ⇒ Object

Return true if fix equals other numerically.

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


2225
2226
2227
# File 'numeric.c', line 2225

static VALUE
fix_equal(x, y)
VALUE x, y;

#>(other) ⇒ Boolean

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

Returns:

  • (Boolean)


2267
2268
2269
# File 'numeric.c', line 2267

static VALUE
fix_gt(x, y)
VALUE x, y;

#>=(other) ⇒ Boolean

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

Returns:

  • (Boolean)


2290
2291
2292
# File 'numeric.c', line 2290

static VALUE
fix_ge(x, y)
VALUE x, y;

#>>(count) ⇒ Integer

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

Returns:



2462
2463
2464
# File 'numeric.c', line 2462

static VALUE
fix_rshift(x, y)
VALUE x, y;

#[](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

Returns:

  • (0, 1)


2497
2498
2499
# File 'numeric.c', line 2497

static VALUE
fix_aref(fix, idx)
VALUE fix, idx;

#^(other) ⇒ Integer

Bitwise EXCLUSIVE OR.

Returns:



2415
2416
2417
# File 'numeric.c', line 2415

static VALUE
fix_xor(x, y)
VALUE x, y;

#absaFixnum

Returns the absolute value of fix.

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

Returns:

  • (aFixnum)


2554
2555
2556
# File 'numeric.c', line 2554

static VALUE
fix_abs(fix)
VALUE fix;

#/(numeric) ⇒ Object #div(numeric) ⇒ Object

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



2125
2126
2127
# File 'numeric.c', line 2125

static VALUE
fix_div(x, y)
VALUE x, y;

#divmod(numeric) ⇒ Array

See Numeric#divmod.

Returns:



2166
2167
2168
# File 'numeric.c', line 2166

static VALUE
fix_divmod(x, y)
VALUE x, y;

#id2nameString?

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:



2580
2581
2582
# File 'numeric.c', line 2580

static VALUE
fix_id2name(fix)
VALUE fix;

#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric

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

Overloads:



2147
2148
2149
# File 'numeric.c', line 2147

static VALUE
fix_mod(x, y)
VALUE x, y;

#quo(numeric) ⇒ Float

Returns the floating point result of dividing fix by numeric.

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

Returns:



2105
2106
2107
# File 'numeric.c', line 2105

static VALUE
fix_quo(x, y)
VALUE x, y;

#sizeFixnum

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

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

Returns:



2627
2628
2629
# File 'numeric.c', line 2627

static VALUE
fix_size(fix)
VALUE fix;

#to_fFloat

Converts fix to a Float.

Returns:



2532
2533
2534
# File 'numeric.c', line 2532

static VALUE
fix_to_f(num)
VALUE num;

#to_s(base = 10) ⇒ aString

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:

  • (aString)


1949
1950
1951
# File 'numeric.c', line 1949

static VALUE
fix_to_s(argc, argv, x)
int argc;

#to_symaSymbol

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

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

Returns:

  • (aSymbol)


2602
2603
2604
# File 'numeric.c', line 2602

static VALUE
fix_to_sym(fix)
VALUE fix;

#zero?Boolean

Returns true if fix is zero.

Returns:

  • (Boolean)


2759
2760
2761
# File 'numeric.c', line 2759

static VALUE
fix_zero_p(num)
VALUE num;

#|(other) ⇒ Integer

Bitwise OR.

Returns:



2395
2396
2397
# File 'numeric.c', line 2395

static VALUE
fix_or(x, y)
VALUE x, y;

#~Integer

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

Returns:



2358
2359
2360
# File 'numeric.c', line 2358

static VALUE
fix_rev(num)
VALUE num;