HoldsInteger values. You cannot add asingleton method to anInteger object, anyattempt to do so will raise aTypeError.
The version of loaded GMP.
Returns the integer square root of the non-negative integern
,i.e. the largest non-negative integer less than or equal to the square rootofn
.
Integer.sqrt(0)#=> 0Integer.sqrt(1)#=> 1Integer.sqrt(24)#=> 4Integer.sqrt(25)#=> 5Integer.sqrt(10**400)#=> 10**200
Equivalent toMath.sqrt(n).floor
, except that the result ofthe latter code may differ from the true value due to the limited precisionof floating point arithmetic.
Integer.sqrt(10**46)#=> 100000000000000000000000Math.sqrt(10**46).floor#=> 99999999999999991611392 (!)
Ifn
is not anInteger, it isconverted to anInteger first. Ifn
is negative, aMath::DomainError israised.
static VALUErb_int_s_isqrt(VALUE self, VALUE num){ unsigned long n, sq; num = rb_to_int(num); if (FIXNUM_P(num)) { if (FIXNUM_NEGATIVE_P(num)) { domain_error("isqrt"); } n = FIX2ULONG(num); sq = rb_ulong_isqrt(n); return LONG2FIX(sq); } else { size_t biglen; if (RBIGNUM_NEGATIVE_P(num)) { domain_error("isqrt"); } biglen = BIGNUM_LEN(num); if (biglen == 0) return INT2FIX(0);#if SIZEOF_BDIGIT <= SIZEOF_LONG /* short-circuit */ if (biglen == 1) { n = BIGNUM_DIGITS(num)[0]; sq = rb_ulong_isqrt(n); return ULONG2NUM(sq); }#endif return rb_big_isqrt(num); }}
Returnsint
moduloother
.
SeeNumeric#divmod for moreinformation.
VALUErb_int_modulo(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_mod(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_modulo(x, y); } return num_modulo(x, y);}
Bitwise AND.
VALUErb_int_and(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_and(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_and(x, y); } return Qnil;}
Performs multiplication: the class of the resulting object depends on theclass ofnumeric
.
VALUErb_int_mul(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_mul(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_mul(x, y); } return rb_num_coerce_bin(x, y, '*');}
Raisesint
to the power ofnumeric
, which may benegative or fractional. The result may be anInteger, aFloat, aRational, or a complex number.
2**3#=> 82**-1#=> (1/2)2**0.5#=> 1.4142135623730951(-1)**0.5#=> (0.0+1.0i)123456789**2#=> 15241578750190521123456789**1.2#=> 5126464716.0993185123456789**-2#=> (1/15241578750190521)
VALUErb_int_pow(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_pow(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_pow(x, y); } return Qnil;}
Performs addition: the class of the resulting object depends on the classofnumeric
.
VALUErb_int_plus(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_plus(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_plus(x, y); } return rb_num_coerce_bin(x, y, '+');}
Performs subtraction: the class of the resulting object depends on theclass ofnumeric
.
VALUErb_int_minus(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_minus(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_minus(x, y); } return rb_num_coerce_bin(x, y, '-');}
Returnsint
, negated.
# File integer.rb, line 6def-@Primitive.attr!'inline'Primitive.cexpr!'rb_int_uminus(self)'end
Performs division: the class of the resulting object depends on the classofnumeric
.
VALUErb_int_div(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_div(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_div(x, y); } return Qnil;}
Returnstrue
if the value ofint
is less thanthat ofreal
.
static VALUEint_lt(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_lt(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_lt(x, y); } return Qnil;}
Returnsint
shifted leftcount
positions, orright ifcount
is negative.
VALUErb_int_lshift(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return rb_fix_lshift(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_lshift(x, y); } return Qnil;}
Returnstrue
if the value ofint
is less than orequal to that ofreal
.
static VALUEint_le(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_le(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_le(x, y); } return Qnil;}
Comparison—Returns -1, 0, or +1 depending on whetherint
isless than, equal to, or greater thannumeric
.
This is the basis for the tests in theComparable module.
nil
is returned if the two values are incomparable.
VALUErb_int_cmp(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_cmp(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_cmp(x, y); } else { rb_raise(rb_eNotImpError, "need to define `<=>' in %s", rb_obj_classname(x)); }}
Returnstrue
ifint
equalsother
numerically. Contrast this withNumeric#eql?, which requiresother
to be anInteger.
1==2#=> false1==1.0#=> true
VALUErb_int_equal(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_equal(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_eq(x, y); } return Qnil;}
Returnstrue
ifint
equalsother
numerically. Contrast this withNumeric#eql?, which requiresother
to be anInteger.
1==2#=> false1==1.0#=> true
VALUErb_int_equal(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_equal(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_eq(x, y); } return Qnil;}
Returnstrue
if the value ofint
is greater thanthat ofreal
.
VALUErb_int_gt(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_gt(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_gt(x, y); } return Qnil;}
Returnstrue
if the value ofint
is greater thanor equal to that ofreal
.
VALUErb_int_ge(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_ge(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_ge(x, y); } return Qnil;}
Returnsint
shifted rightcount
positions, orleft ifcount
is negative.
static VALUErb_int_rshift(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return rb_fix_rshift(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_rshift(x, y); } return Qnil;}
Bit Reference—Returns then
th bit in the binary representationofint
, whereint[0]
is the least significantbit.
a =0b1100110010101030.downto(0) {|n|printa[n] }#=> 0000000000000000011001100101010a =9**1550.downto(0) {|n|printa[n] }#=> 000101110110100000111000011110010100111100010111001
In principle,n[i]
is equivalent to(n >> i) &1
. Thus, any negative index always returns zero:
p255[-1]#=> 0
Range operationsn[i, len]
andn[i..j]
are naturally extended.
n[i, len]
equals to(n >> i) & ((1 <<len) - 1)
.
n[i..j]
equals to(n >> i) & ((1 << (j -i + 1)) - 1)
.
n[i...j]
equals to(n >> i) & ((1 << (j- i)) - 1)
.
n[i..]
equals to(n >> i)
.
n[..j]
is zero ifn & ((1 << (j + 1)) -1)
is zero. Otherwise, raises anArgumentError.
n[...j]
is zero ifn & ((1 << j) - 1)
is zero. Otherwise, raises anArgumentError.
Note that range operation may exhaust memory. For example,-1[0,1000000000000]
will raiseNoMemoryError.
static VALUEint_aref(int const argc, VALUE * const argv, VALUE const num){ rb_check_arity(argc, 1, 2); if (argc == 2) { return int_aref2(num, argv[0], argv[1]); } return int_aref1(num, argv[0]); return Qnil;}
Bitwise EXCLUSIVE OR.
static VALUEint_xor(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_xor(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_xor(x, y); } return Qnil;}
# File integer.rb, line 27defabsPrimitive.attr!'inline'Primitive.cexpr!'rb_int_abs(self)'end
Returnstrue
if all bits ofint & mask
are 1.
static VALUEint_allbits_p(VALUE num, VALUE mask){ mask = rb_to_int(mask); return rb_int_equal(rb_int_and(num, mask), mask);}
Returnstrue
if any bits ofint & mask
are 1.
static VALUEint_anybits_p(VALUE num, VALUE mask){ mask = rb_to_int(mask); return int_zero_p(rb_int_and(num, mask)) ? Qfalse : Qtrue;}
Returns the number of bits of the value ofint
.
“Number of bits” means the bit position of the highest bit which isdifferent from the sign bit (where the least significant bit has bitposition 1). If there is no such bit (zero or minus one), zero is returned.
I.e. this method returnsceil(log2(int < 0 ? -int : int+1)).
(-2**1000-1).bit_length#=> 1001(-2**1000).bit_length#=> 1000(-2**1000+1).bit_length#=> 1000(-2**12-1).bit_length#=> 13(-2**12).bit_length#=> 12(-2**12+1).bit_length#=> 12-0x101.bit_length#=> 9-0x100.bit_length#=> 8-0xff.bit_length#=> 8-2.bit_length#=> 1-1.bit_length#=> 00.bit_length#=> 01.bit_length#=> 10xff.bit_length#=> 80x100.bit_length#=> 9(2**12-1).bit_length#=> 12(2**12).bit_length#=> 13(2**12+1).bit_length#=> 13(2**1000-1).bit_length#=> 1000(2**1000).bit_length#=> 1001(2**1000+1).bit_length#=> 1001
This method can be used to detect overflow inArray#pack as follows:
ifn.bit_length<32 [n].pack("l")# no overflowelseraise"overflow"end
# File integer.rb, line 73defbit_lengthPrimitive.attr!'inline'Primitive.cexpr!'rb_int_bit_length(self)'end
Returns the smallest number greater than or equal toint
witha precision ofndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with atleastndigits.abs
trailing zeros.
Returnsself
whenndigits
is zero or positive.
1.ceil#=> 11.ceil(2)#=> 118.ceil(-1)#=> 20(-18).ceil(-1)#=> -10
static VALUEint_ceil(int argc, VALUE* argv, VALUE num){ int ndigits; if (!rb_check_arity(argc, 0, 1)) return num; ndigits = NUM2INT(argv[0]); if (ndigits >= 0) { return num; } return rb_int_ceil(num, ndigits);}
Returns a string containing the character represented by theint
's value according toencoding
.
65.chr#=> "A"230.chr#=> "\xE6"255.chr(Encoding::UTF_8)#=> "\u00FF"
static VALUEint_chr(int argc, VALUE *argv, VALUE num){ char c; unsigned int i; rb_encoding *enc; if (rb_num_to_uint(num, &i) == 0) { } else if (FIXNUM_P(num)) { rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(num)); } else { rb_raise(rb_eRangeError, "bignum out of char range"); } switch (argc) { case 0: if (0xff < i) { enc = rb_default_internal_encoding(); if (!enc) { rb_raise(rb_eRangeError, "%u out of char range", i); } goto decode; } c = (char)i; if (i < 0x80) { return rb_usascii_str_new(&c, 1); } else { return rb_str_new(&c, 1); } case 1: break; default: rb_error_arity(argc, 0, 1); } enc = rb_to_encoding(argv[0]); if (!enc) enc = rb_ascii8bit_encoding(); decode: return rb_enc_uint_chr(i, enc);}
Returns an array with both anumeric
and abig
represented as Bignum objects.
This is achieved by convertingnumeric
to a Bignum.
ATypeError is raised if thenumeric
is not a Fixnum or Bignum type.
(0x3FFFFFFFFFFFFFFF+1).coerce(42)#=> [42, 4611686018427387904]
static VALUErb_int_coerce(VALUE x, VALUE y){ if (RB_INTEGER_TYPE_P(y)) { return rb_assoc_new(y, x); } else { x = rb_Float(x); y = rb_Float(y); return rb_assoc_new(y, x); }}
Returns 1.
static VALUEinteger_denominator(VALUE self){ return INT2FIX(1);}
Returns the digits ofint
's place-value representationwith radixbase
(default: 10). The digits are returned as anarray with the least significant digit as the first array element.
base
must be greater than or equal to 2.
12345.digits#=> [5, 4, 3, 2, 1]12345.digits(7)#=> [4, 6, 6, 0, 5]12345.digits(100)#=> [45, 23, 1]-12345.digits(7)#=> Math::DomainError
static VALUErb_int_digits(int argc, VALUE *argv, VALUE num){ VALUE base_value; long base; if (rb_num_negative_p(num)) rb_raise(rb_eMathDomainError, "out of domain"); if (rb_check_arity(argc, 0, 1)) { base_value = rb_to_int(argv[0]); if (!RB_INTEGER_TYPE_P(base_value)) rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)", rb_obj_classname(argv[0])); if (RB_TYPE_P(base_value, T_BIGNUM)) return rb_int_digits_bigbase(num, base_value); base = FIX2LONG(base_value); if (base < 0) rb_raise(rb_eArgError, "negative radix"); else if (base < 2) rb_raise(rb_eArgError, "invalid radix %ld", base); } else base = 10; if (FIXNUM_P(num)) return rb_fix_digits(num, base); else if (RB_TYPE_P(num, T_BIGNUM)) return rb_int_digits_bigbase(num, LONG2FIX(base)); return Qnil;}
Performs integer division: returns the integer result of dividingint
bynumeric
.
VALUErb_int_idiv(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_idiv(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_idiv(x, y); } return num_div(x, y);}
SeeNumeric#divmod.
VALUErb_int_divmod(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_divmod(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_divmod(x, y); } return Qnil;}
Iterates the given block, passing in decreasing values fromint
down to and includinglimit
.
If no block is given, anEnumerator isreturned instead.
5.downto(1) {|n|printn,".. " }puts"Liftoff!"#=> "5.. 4.. 3.. 2.. 1.. Liftoff!"
static VALUEint_downto(VALUE from, VALUE to){ RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size); if (FIXNUM_P(from) && FIXNUM_P(to)) { long i, end; end = FIX2LONG(to); for (i=FIX2LONG(from); i >= end; i--) { rb_yield(LONG2FIX(i)); } } else { VALUE i = from, c; while (!(c = rb_funcall(i, '<', 1, to))) { rb_yield(i); i = rb_funcall(i, '-', 1, INT2FIX(1)); } if (NIL_P(c)) rb_cmperr(i, to); } return from;}
Returnstrue
ifint
is an even number.
# File integer.rb, line 82defeven?Primitive.attr!'inline'Primitive.cexpr!'rb_int_even_p(self)'end
Returns the floating point result of dividingint
bynumeric
.
654321.fdiv(13731)#=> 47.652829364212366654321.fdiv(13731.24)#=> 47.65199646936475-654321.fdiv(13731)#=> -47.652829364212366
VALUErb_int_fdiv(VALUE x, VALUE y){ if (RB_INTEGER_TYPE_P(x)) { return DBL2NUM(rb_int_fdiv_double(x, y)); } return Qnil;}
Returns the largest number less than or equal toint
with aprecision ofndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with atleastndigits.abs
trailing zeros.
Returnsself
whenndigits
is zero or positive.
1.floor#=> 11.floor(2)#=> 118.floor(-1)#=> 10(-18).floor(-1)#=> -20
static VALUEint_floor(int argc, VALUE* argv, VALUE num){ int ndigits; if (!rb_check_arity(argc, 0, 1)) return num; ndigits = NUM2INT(argv[0]); if (ndigits >= 0) { return num; } return rb_int_floor(num, ndigits);}
Returns the greatest common divisor of the two integers. The result isalways positive. 0.gcd(x) and x.gcd(0) return x.abs.
36.gcd(60)#=> 122.gcd(2)#=> 23.gcd(-7)#=> 1((1<<31)-1).gcd((1<<61)-1)#=> 1
VALUErb_gcd(VALUE self, VALUE other){ other = nurat_int_value(other); return f_gcd(self, other);}
Returns an array with the greatest common divisor and the least commonmultiple of the two integers, [gcd, lcm].
36.gcdlcm(60)#=> [12, 180]2.gcdlcm(2)#=> [2, 2]3.gcdlcm(-7)#=> [1, 21]((1<<31)-1).gcdlcm((1<<61)-1)#=> [1, 4951760154835678088235319297]
VALUErb_gcdlcm(VALUE self, VALUE other){ other = nurat_int_value(other); return rb_assoc_new(f_gcd(self, other), f_lcm(self, other));}
Sinceint
is already anInteger,this always returnstrue
.
# File integer.rb, line 91definteger?returntrueend
Returns the least common multiple of the two integers. The result is alwayspositive. 0.lcm(x) and x.lcm(0) return zero.
36.lcm(60)#=> 1802.lcm(2)#=> 23.lcm(-7)#=> 21((1<<31)-1).lcm((1<<61)-1)#=> 4951760154835678088235319297
VALUErb_lcm(VALUE self, VALUE other){ other = nurat_int_value(other); return f_lcm(self, other);}
# File integer.rb, line 95defmagnitudePrimitive.attr!'inline'Primitive.cexpr!'rb_int_abs(self)'end
Returnsint
moduloother
.
SeeNumeric#divmod for moreinformation.
VALUErb_int_modulo(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_mod(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_modulo(x, y); } return num_modulo(x, y);}
Returns the successor ofint
, i.e. theInteger equal toint+1
.
1.next#=> 2(-1).next#=> 01.succ#=> 2(-1).succ#=> 0
VALUErb_int_succ(VALUE num){ if (FIXNUM_P(num)) { long i = FIX2LONG(num) + 1; return LONG2NUM(i); } if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_plus(num, INT2FIX(1)); } return num_funcall1(num, '+', INT2FIX(1));}
Returnstrue
if no bits ofint & mask
are 1.
static VALUEint_nobits_p(VALUE num, VALUE mask){ mask = rb_to_int(mask); return int_zero_p(rb_int_and(num, mask));}
Returns self.
static VALUEinteger_numerator(VALUE self){ return self;}
Returnstrue
ifint
is an odd number.
# File integer.rb, line 104defodd?Primitive.attr!'inline'Primitive.cexpr!'rb_int_odd_p(self)'end
Returns theint
itself.
97.ord#=> 97
This method is intended for compatibility to character literals in Ruby1.9.
For example,?a.ord
returns 97 both in 1.8 and 1.9.
# File integer.rb, line 120defordreturnselfend
Returns (modular) exponentiation as:
a.pow(b)#=> same as a**ba.pow(b,m)#=> same as (a**b) % m, but avoids huge temporary values
VALUErb_int_powm(int const argc, VALUE * const argv, VALUE const num){ rb_check_arity(argc, 1, 2); if (argc == 1) { return rb_int_pow(num, argv[0]); } else { VALUE const a = num; VALUE const b = argv[0]; VALUE m = argv[1]; int nega_flg = 0; if ( ! RB_INTEGER_TYPE_P(b)) { rb_raise(rb_eTypeError, "Integer#pow() 2nd argument not allowed unless a 1st argument is integer"); } if (rb_int_negative_p(b)) { rb_raise(rb_eRangeError, "Integer#pow() 1st argument cannot be negative when 2nd argument specified"); } if (!RB_INTEGER_TYPE_P(m)) { rb_raise(rb_eTypeError, "Integer#pow() 2nd argument not allowed unless all arguments are integers"); } if (rb_int_negative_p(m)) { m = rb_int_uminus(m); nega_flg = 1; } if (FIXNUM_P(m)) { long const half_val = (long)HALF_LONG_MSB; long const mm = FIX2LONG(m); if (!mm) rb_num_zerodiv(); if (mm == 1) return INT2FIX(0); if (mm <= half_val) { return int_pow_tmp1(rb_int_modulo(a, m), b, mm, nega_flg); } else { return int_pow_tmp2(rb_int_modulo(a, m), b, mm, nega_flg); } } else { if (rb_bigzero_p(m)) rb_num_zerodiv(); if (bignorm(m) == INT2FIX(1)) return INT2FIX(0); return int_pow_tmp3(rb_int_modulo(a, m), b, m, nega_flg); } } UNREACHABLE_RETURN(Qnil);}
Returns the predecessor ofint
, i.e. theInteger equal toint-1
.
1.pred#=> 0(-1).pred#=> -2
static VALUErb_int_pred(VALUE num){ if (FIXNUM_P(num)) { long i = FIX2LONG(num) - 1; return LONG2NUM(i); } if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_minus(num, INT2FIX(1)); } return num_funcall1(num, '-', INT2FIX(1));}
Returns the value as a rational. The optional argumenteps
isalways ignored.
static VALUEinteger_rationalize(int argc, VALUE *argv, VALUE self){ rb_check_arity(argc, 0, 1); return integer_to_r(self);}
Returns the remainder after dividingint
bynumeric
.
x.remainder(y)
meansx-y*(x/y).truncate
.
5.remainder(3)#=> 2-5.remainder(3)#=> -25.remainder(-3)#=> 2-5.remainder(-3)#=> -25.remainder(1.5)#=> 0.5
SeeNumeric#divmod.
static VALUEint_remainder(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return num_remainder(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_remainder(x, y); } return Qnil;}
Returnsint
rounded to the nearest value with a precision ofndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with atleastndigits.abs
trailing zeros.
Returnsself
whenndigits
is zero or positive.
1.round#=> 11.round(2)#=> 115.round(-1)#=> 20(-15).round(-1)#=> -20
The optionalhalf
keyword argument is available similar toFloat#round.
25.round(-1,half: :up)#=> 3025.round(-1,half: :down)#=> 2025.round(-1,half: :even)#=> 2035.round(-1,half: :up)#=> 4035.round(-1,half: :down)#=> 3035.round(-1,half: :even)#=> 40(-25).round(-1,half: :up)#=> -30(-25).round(-1,half: :down)#=> -20(-25).round(-1,half: :even)#=> -20
static VALUEint_round(int argc, VALUE* argv, VALUE num){ int ndigits; int mode; VALUE nd, opt; if (!rb_scan_args(argc, argv, "01:", &nd, &opt)) return num; ndigits = NUM2INT(nd); mode = rb_num_get_rounding_option(opt); if (ndigits >= 0) { return num; } return rb_int_round(num, ndigits, mode);}
Returns the number of bytes in the machine representation ofint
(machine dependent).
1.size#=> 8-1.size#=> 82147483647.size#=> 8(256**10-1).size#=> 10(256**20-1).size#=> 20(256**40-1).size#=> 40
static VALUEint_size(VALUE num){ if (FIXNUM_P(num)) { return fix_size(num); } else if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_size_m(num); } return Qnil;}
Returns the successor ofint
, i.e. theInteger equal toint+1
.
1.next#=> 2(-1).next#=> 01.succ#=> 2(-1).succ#=> 0
VALUErb_int_succ(VALUE num){ if (FIXNUM_P(num)) { long i = FIX2LONG(num) + 1; return LONG2NUM(i); } if (RB_TYPE_P(num, T_BIGNUM)) { return rb_big_plus(num, INT2FIX(1)); } return num_funcall1(num, '+', INT2FIX(1));}
Iterates the given blockint
times, passing in values fromzero toint - 1
.
If no block is given, anEnumerator isreturned instead.
5.times {|i|printi," " }#=> 0 1 2 3 4
static VALUEint_dotimes(VALUE num){ RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size); if (FIXNUM_P(num)) { long i, end; end = FIX2LONG(num); for (i=0; i<end; i++) { rb_yield_1(LONG2FIX(i)); } } else { VALUE i = INT2FIX(0); for (;;) { if (!RTEST(rb_funcall(i, '<', 1, num))) break; rb_yield(i); i = rb_funcall(i, '+', 1, INT2FIX(1)); } } return num;}
Convertsint
to aFloat. Ifint
doesn't fit in aFloat, theresult is infinity.
static VALUEint_to_f(VALUE num){ double val; if (FIXNUM_P(num)) { val = (double)FIX2LONG(num); } else if (RB_TYPE_P(num, T_BIGNUM)) { val = rb_big2dbl(num); } else { rb_raise(rb_eNotImpError, "Unknown subclass for to_f: %s", rb_obj_classname(num)); } return DBL2NUM(val);}
Sinceint
is already anInteger,returnsself
.
# File integer.rb, line 138defto_intreturnselfend
Returns the value as a rational.
1.to_r#=> (1/1)(1<<64).to_r#=> (18446744073709551616/1)
static VALUEinteger_to_r(VALUE self){ return rb_rational_new1(self);}
Returns a string containing the place-value representation ofint
with radixbase
(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"78546939656932.to_s(36)#=> "rubyrules"
static VALUEint_to_s(int argc, VALUE *argv, VALUE x){ int base; if (rb_check_arity(argc, 0, 1)) base = NUM2INT(argv[0]); else base = 10; return rb_int2str(x, base);}
Returnsint
truncated (toward zero) to a precision ofndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with atleastndigits.abs
trailing zeros.
Returnsself
whenndigits
is zero or positive.
1.truncate#=> 11.truncate(2)#=> 118.truncate(-1)#=> 10(-18).truncate(-1)#=> -10
static VALUEint_truncate(int argc, VALUE* argv, VALUE num){ int ndigits; if (!rb_check_arity(argc, 0, 1)) return num; ndigits = NUM2INT(argv[0]); if (ndigits >= 0) { return num; } return rb_int_truncate(num, ndigits);}
Iterates the given block, passing in integer values fromint
up to and includinglimit
.
If no block is given, anEnumerator isreturned instead.
5.upto(10) {|i|printi," " }#=> 5 6 7 8 9 10
static VALUEint_upto(VALUE from, VALUE to){ RETURN_SIZED_ENUMERATOR(from, 1, &to, int_upto_size); if (FIXNUM_P(from) && FIXNUM_P(to)) { long i, end; end = FIX2LONG(to); for (i = FIX2LONG(from); i <= end; i++) { rb_yield(LONG2FIX(i)); } } else { VALUE i = from, c; while (!(c = rb_funcall(i, '>', 1, to))) { rb_yield(i); i = rb_funcall(i, '+', 1, INT2FIX(1)); } ensure_cmp(c, i, to); } return from;}
Returnstrue
ifint
has a zero value.
# File integer.rb, line 146defzero?Primitive.attr!'inline'Primitive.cexpr!'rb_int_zero_p(self)'end
Bitwise OR.
static VALUEint_or(VALUE x, VALUE y){ if (FIXNUM_P(x)) { return fix_or(x, y); } else if (RB_TYPE_P(x, T_BIGNUM)) { return rb_big_or(x, y); } return Qnil;}
One's complement: returns a number where each bit is flipped.
Inverts the bits in anInteger. As integers areconceptually of infinite length, the result acts as if it had an infinitenumber of one bits to the left. In hex representations, this is displayedas two periods to the left of the digits.
sprintf("%X",~0x1122334455)#=> "..FEEDDCCBBAA"
# File integer.rb, line 22def~Primitive.attr!'inline'Primitive.cexpr!'rb_int_comp(self)'end
This page was generated for Ruby 3.0.0
Generated with Ruby-doc Rdoc Generator 0.42.0.