Ruby 4.0 リファレンスマニュアル

class Integer

[edit]

dynamic include: JSON::Generator::GeneratorMethods::Integer (by json)

要約

整数クラスです。

整数オブジェクトに特異メソッドを追加する事はできません。追加した場合、 TypeError が発生します。

かつて Integer クラスのエイリアスであった Fixnum と Bignum は 3.2 で削除されました。

目次

特異メソッド
インスタンスメソッド
追加されるメソッド

継承しているメソッド

Numericから継承しているメソッド
Comparableから継承しているメソッド

動的includeで追加されるメソッド

JSON::Generator::GeneratorMethods::Integer (by json)

特異メソッド

sqrt(n) -> IntegerRuby 2.5.0 から[permalink][rdoc][edit]

非負整数 n の整数の平方根を返します。すなわち n の平方根以下の最大の非負整数を返します。

[PARAM] n:
非負整数。Integer ではない場合は、最初に Integer に変換されます。
[EXCEPTION] Math::DomainError:
n が負の整数の時に発生します。
p Integer.sqrt(0)      # => 0
p Integer.sqrt(1)      # => 1
p Integer.sqrt(24)     # => 4
p Integer.sqrt(25)     # => 5
p Integer.sqrt(10**400) == 10**200 # => true

Math.sqrt(n).floor と同等ですが、後者は浮動小数点数の精度の限界によって真の値とは違う結果になることがあります。

p Integer.sqrt(10**46)   #=> 100000000000000000000000
p Math.sqrt(10**46).floor  #=>  99999999999999991611392 (!)

[SEE_ALSO] Math.#sqrt

try_convert(obj) -> Integer | nilRuby 3.1 から[permalink][rdoc][edit]

obj を Integer に変換しようと試みます。変換には Object#to_int メソッドが使われます。

Integer ならそのままobjを返します。そうでなければ obj.to_int の結果を返すか、nil が返されます。

[PARAM] obj:
変換する任意のオブジェクト
[RETURN]
Integer または nil
[EXCEPTION] TypeError:
to_int が Integer を返さなかった場合に発生します。
p Integer.try_convert(1)  # => 1
p Integer.try_convert(1.25) # => 1
p Integer.try_convert([]) # => nil

インスタンスメソッド

self % other -> NumericRuby 2.4.0 から[permalink][rdoc][edit]
modulo(other) -> Numeric

算術演算子。剰余を計算します。

p 13 % 4  # =>  1
p 13 % -4 # => -3
p -13 % 4 # =>  3
p -13 % -4  # => -1
[PARAM] other:
二項演算の右側の引数(対象)
[RETURN]
計算結果
self & other -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]

ビット二項演算子。論理積を計算します。

[PARAM] other:
数値
p 1 & 1  # => 1
p 2 & 3  # => 2
self * other -> NumericRuby 2.4.0 から[permalink][rdoc][edit]

算術演算子。積を計算します。

[PARAM] other:
二項演算の右側の引数(対象)
[RETURN]
計算結果
p 2 * 3 # => 6
self ** other -> Numeric[permalink][rdoc][edit]
pow(other) -> NumericRuby 2.5.0 から
pow(other, modulo) -> IntegerRuby 2.5.0 から

算術演算子。冪(べき乗)を計算します。

[PARAM] other:
二項演算の右側の引数(対象)
[PARAM] modulo:
指定すると、計算途中に巨大な値を生成せずに (self**other) % modulo と同じ結果を返します。
[RETURN]
計算結果
[EXCEPTION] TypeError:
2引数 pow で Integer 以外を指定した場合に発生します。
[EXCEPTION] RangeError:
2引数 pow で other に負の数を指定した場合に発生します。
[EXCEPTION] ArgumentError:
計算結果が巨大になりすぎる場合に発生します。
p 2 ** 3 # => 8
p 2 ** 0 # => 1
p 0 ** 0 # => 1
p 3.pow(3,  8)  # =>  3
p 3.pow(3, -8)  # => -5
p 3.pow(2, -2)  # => -1
p -3.pow(3,  8) # =>  5
p -3.pow(3, -8) # => -3
p 5.pow(2, -8)  # => -7

計算結果が巨大すぎるときは ArgumentError が発生します。

計算結果が巨大すぎる例
p 100**9999999999999999999
# => exponent is too large (ArgumentError)

判定の閾値は変わりえます。

[SEE_ALSO] BigDecimal#power

self + other -> NumericRuby 2.4.0 から[permalink][rdoc][edit]

算術演算子。和を計算します。

[PARAM] other:
二項演算の右側の引数(対象)
[RETURN]
計算結果
p 3 + 4 # => 7
self - other -> NumericRuby 2.4.0 から[permalink][rdoc][edit]

算術演算子。差を計算します。

[PARAM] other:
二項演算の右側の引数(対象)
[RETURN]
計算結果
p 4 - 1 #=> 3
- self -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]

単項演算子の - です。 self の符号を反転させたものを返します。

p(- 10) # => -10
p(- -10) # => 10
self / other -> Numeric[permalink][rdoc][edit]

除算の算術演算子。

other が Integer の場合、整商(整数の商)を Integer で返します。普通の商(剰余を考えない商)を越えない最大の整数をもって整商とします。

other が Float、Rational、Complex の場合、普通の商を other と同じクラスのインスタンスで返します。

[PARAM] other:
二項演算の右側の引数(対象)
[RETURN]
計算結果
p 7 / 2 # => 3
p 7 / -2 # => -4
p 7 / 2.0 # => 3.5
p 7 / 2r # => (7/2)
p 7 / (2+0i) # => ((7/2)+0i)

begin
  2 / 0
rescue => e
  e # => #<ZeroDivisionError: divided by 0>
end

[SEE_ALSO] Integer#div, Integer#fdiv, Numeric#quo

self < other -> boolRuby 2.4.0 から[permalink][rdoc][edit]

比較演算子。数値として小さいか判定します。

[PARAM] other:
比較対象の数値
[RETURN]
self よりも other が大きい場合 true を返します。そうでなければ false を返します。
p 1 < 1  # => false
p 1 < 2  # => true
self << bits -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]

シフト演算子。bits だけビットを左にシフトします。

[PARAM] bits:
シフトさせるビット数
printf("%#b\n", 0b0101 << 1) # => 0b1010
p -1 << 1 # => -2
self <= other -> boolRuby 2.4.0 から[permalink][rdoc][edit]

比較演算子。数値として等しいまたは小さいか判定します。

[PARAM] other:
比較対象の数値
[RETURN]
self よりも other の方が大きい場合か、両者が等しい場合 true を返します。そうでなければ false を返します。
p 1 <= 0  # => false
p 1 <= 1  # => true
p 1 <= 2  # => true
self <=> other -> -1 | 0 | 1 | nilRuby 2.4.0 から[permalink][rdoc][edit]

self と other を比較して、self が大きい時に1、等しい時に 0、小さい時に-1、比較できない時に nil を返します。

[PARAM] other:
比較対象の数値
[RETURN]
-1 か 0 か 1 か nil のいずれか
p 1 <=> 2  # => -1
p 1 <=> 1  # => 0
p 2 <=> 1  # => 1
p 2 <=> '' # => nil
self == other -> boolRuby 2.4.0 から[permalink][rdoc][edit]
self === other -> bool

比較演算子。数値として等しいか判定します。

[PARAM] other:
比較対象の数値
[RETURN]
self と other が等しい場合 true を返します。そうでなければ false を返します。
p 1 == 2    # => false
p 1 == 1.0  # => true
self > other -> boolRuby 2.4.0 から[permalink][rdoc][edit]

比較演算子。数値として大きいか判定します。

[PARAM] other:
比較対象の数値
[RETURN]
self よりも other の方が小さい場合 true を返します。そうでなければ false を返します。
p 1 > 0  # => true
p 1 > 1  # => false
self >= other -> boolRuby 2.4.0 から[permalink][rdoc][edit]

比較演算子。数値として等しいまたは大きいか判定します。

[PARAM] other:
比較対象の数値
[RETURN]
self よりも other の方が小さい場合か、両者が等しい場合 true を返します。そうでなければ false を返します。
p 1 >= 0  # => true
p 1 >= 1  # => true
p 1 >= 2  # => false
self >> bits -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]

シフト演算子。bits だけビットを右にシフトします。

右シフトは、符号ビット(最上位ビット(MSB))が保持されます。 bitsが実数の場合、小数点以下を切り捨てた値でシフトします。

[PARAM] bits:
シフトさせるビット数
printf("%#b\n", 0b0101 >> 1) # => 0b10
p -1 >> 1                    # => -1
self[nth] -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]
self[nth, len] -> Integer
self[range] -> Integer

nth 番目のビット(最下位ビット(LSB)が 0 番目)が立っている時 1 を、そうでなければ 0 を返します。

[PARAM] nth:
何ビット目を指すかの数値
[PARAM] len:
何ビット分を返すか
[PARAM] range:
返すビットの範囲
[RETURN]
self[nth] は 1 か 0
[RETURN]
self[i, len] は (n >> i) & ((1 << len) - 1) と同じ
[RETURN]
self[i..j] は (n >> i) & ((1 << (j - i + 1)) - 1) と同じ
[RETURN]
self[i...j] は (n >> i) & ((1 << (j - i)) - 1) と同じ
[RETURN]
self[i..] は (n >> i) と同じ
[RETURN]
self[..j] は n & ((1 << (j + 1)) - 1) が 0 なら 0
[RETURN]
self[...j] は n & ((1 << j) - 1) が 0 なら 0
[EXCEPTION] ArgumentError:
self[..j] で n & ((1 << (j + 1)) - 1) が 0 以外のとき
[EXCEPTION] ArgumentError:
self[...j] で n & ((1 << j) - 1) が 0 以外のとき
a = 0b11001100101010
30.downto(0) {|n| print a[n] }
# => 0000000000000000011001100101010

a = 9**15
50.downto(0) {|n| print a[n] }
# => 000101110110100000111000011110010100111100010111001

n[i] は (n >> i) & 1 と等価なので、負のインデックスは常に 0 を返します。

p 255[-1] # => 0
複数ビットの例
p 0b01001101[2, 4]  #=> 0b0011
p 0b01001100[2..5]  #=> 0b0011
p 0b01001100[2...6] #=> 0b0011
#   ^^^^

self[nth]=bit (つまりビットの修正) がないのは、Numeric 関連クラスが immutable であるためです。

self ^ other -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]

ビット二項演算子。排他的論理和を計算します。

[PARAM] other:
数値
p 1 ^ 1  # => 0
p 2 ^ 3  # => 1
abs -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]
magnitude -> Integer

self の絶対値を返します。

p -12345.abs # => 12345
p 12345.abs  # => 12345
p -1234567890987654321.abs # => 1234567890987654321
allbits?(mask) -> boolRuby 2.5.0 から[permalink][rdoc][edit]

mask で 1 が立っているビットがすべて self でも 1 なら true を返します。

self & mask == mask と等価です。

[PARAM] mask:
ビットマスクを整数で指定します。
p 42.allbits?(42)                 # => true
p 0b1010_1010.allbits?(0b1000_0010) # => true
p 0b1010_1010.allbits?(0b1000_0001) # => false
p 0b1000_0010.allbits?(0b1010_1010) # => false

[SEE_ALSO] Integer#anybits?

[SEE_ALSO] Integer#nobits?

anybits?(mask) -> boolRuby 2.5.0 から[permalink][rdoc][edit]

self & mask のいずれかのビットが 1 なら true を返します。

self & mask != 0 と等価です。

[PARAM] mask:
ビットマスクを整数で指定します。
p 42.anybits?(42)                 # => true
p 0b1010_1010.anybits?(0b1000_0010) # => true
p 0b1010_1010.anybits?(0b1000_0001) # => true
p 0b1000_0010.anybits?(0b0010_1100) # => false

[SEE_ALSO] Integer#allbits?

[SEE_ALSO] Integer#nobits?

bit_length -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]

self を表すのに必要なビット数を返します。

「必要なビット数」とは符号ビットを除く最上位ビットの位置の事を意味します。2**n の場合は n+1 になります。self にそのようなビットがない(0 や -1 である)場合は 0 を返します。

例: ceil(log2(int < 0 ? -int : int+1)) と同じ結果
p (-2**12-1).bit_length   # => 13
p (-2**12).bit_length     # => 12
p (-2**12+1).bit_length   # => 12
p -0x101.bit_length       # => 9
p -0x100.bit_length       # => 8
p -0xff.bit_length        # => 8
p -2.bit_length           # => 1
p -1.bit_length           # => 0
p 0.bit_length            # => 0
p 1.bit_length            # => 1
p 0xff.bit_length         # => 8
p 0x100.bit_length        # => 9
p (2**12-1).bit_length    # => 12
p (2**12).bit_length      # => 13
p (2**12+1).bit_length    # => 13

[SEE_ALSO] Integer#size

ceil(ndigits = 0) -> Integer[permalink][rdoc][edit]

self と等しいかより大きな整数のうち最小のものを返します。

[PARAM] ndigits:
10進数での小数点以下の有効桁数を整数で指定します。負の整数を指定した場合、小数点位置から左に少なくとも n 個の 0 が並びます。
p 1.ceil         # => 1
p 1.ceil(2)      # => 1
p 18.ceil(-1)    # => 20
p (-18).ceil(-1) # => -10

[SEE_ALSO] Numeric#ceil

ceildiv(other) -> IntegerRuby 3.2 から[permalink][rdoc][edit]

self を other で割り、その(剰余を考えない)商を整数に切り上げたものを返します。すなわち、self を other で割った商を q とすると、q 以上で最小の整数を返します。

[PARAM] other:
self を割る数を指定します。
p 3.ceildiv(3)  # => 1
p 4.ceildiv(3)  # => 2
p 5.ceildiv(3)  # => 2
p 3.ceildiv(1.2)  # => 3
p -5.ceildiv(3) # => -1
p -5.ceildiv(-3)  # => 2
chr -> String[permalink][rdoc][edit]
chr(encoding) -> String

self を文字コードとして見た時に、引数で与えたエンコーディング encoding に対応する文字を返します。

p 65.chr
# => "A"
p 12354.chr
# => 'chr': 12354 out of char range (RangeError)

p 12354.chr(Encoding::UTF_8)
# => "あ"
12354.chr(Encoding::EUC_JP)
# ~> RangeError: invalid codepoint 0x3042 in EUC-JP

引数無しで呼ばれた場合は self を US-ASCII、ASCII-8BIT、デフォルト内部エンコーディングの順で優先的に解釈します。

p 0x79.chr.encoding # => #<Encoding:US_ASCII>
p 0x80.chr.encoding # => #<Encoding:ASCII_8BIT>
[PARAM] encoding:
エンコーディングを表すオブジェクト。Encoding::UTF_8、'shift_jis' など。
[RETURN]
一文字からなる文字列
[EXCEPTION] RangeError:
self を与えられたエンコーディングで正しく解釈できない場合に発生します。

[SEE_ALSO] String#ord Encoding.default_internal

denominator -> Integer[permalink][rdoc][edit]

分母(常に1)を返します。

[RETURN]
分母を返します。
p 10.denominator  # => 1
p -10.denominator # => 1

[SEE_ALSO] Integer#numerator

digits -> [Integer]Ruby 2.4.0 から[permalink][rdoc][edit]
digits(base) -> [Integer]

base を基数として self を位取り記数法で表記した数値を配列で返します。 base を指定しない場合の基数は 10 です。

p 16.digits   # => [6, 1]
p 16.digits(16) # => [0, 1]

self は非負整数でなければいけません。非負整数でない場合は、Math::DomainErrorが発生します。

-10.digits  # Math::DomainError: out of domain が発生
[RETURN]
位取り記数法で表した時の数値の配列
[PARAM] base:
基数となる数値。
[EXCEPTION] ArgumentError:
base に正の整数以外を指定した場合に発生します。
[EXCEPTION] Math::DomainError:
非負整数以外に対して呼び出した場合に発生します。
div(other) -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]

整商(整数の商)を返します。普通の商(剰余を考えない商)を越えない最大の整数をもって整商とします。

other が Integer オブジェクトの場合、Integer#/ の結果と一致します。

div に対応する剰余メソッドは modulo です。

[PARAM] other:
二項演算の右側の引数(対象)
[RETURN]
計算結果
p 7.div(2) # => 3
p 7.div(-2) # => -4
p 7.div(2.0) # => 3
p 7.div(2r) # => 3

begin
  2.div(0)
rescue => e
  e # => #<ZeroDivisionError: divided by 0>
end

begin
  2.div(0.0)
rescue => e
  e # => #<ZeroDivisionError: divided by 0>
  # Integer#/ と違い、引数が Float でもゼロで割ることはできない
end

[SEE_ALSO] Integer#fdiv, Integer#/, Integer#modulo

divmod(other) -> [Integer, Numeric]Ruby 2.4.0 から[permalink][rdoc][edit]

self を other で割った商 q と余り r を、 [q, r] という 2 要素の配列にして返します。 商 q は常に整数ですが、余り r は整数であるとは限りません。

[PARAM] other:
self を割る数。

[SEE_ALSO] Numeric#divmod

downto(min) {|n| ... } -> self[permalink][rdoc][edit]
downto(min) -> Enumerator

self から min まで 1 ずつ減らしながらブロックを繰り返し実行します。 self < min であれば何もしません。

[PARAM] min:
数値
[RETURN]
self を返します。
p 5.downto(1) {|i| print i, " " } # => 5 4 3 2 1

[SEE_ALSO] Integer#upto, Numeric#step, Integer#times

even? -> bool[permalink][rdoc][edit]

自身が偶数であれば真を返します。そうでない場合は偽を返します。

p 10.even?  # => true
p 5.even?   # => false
fdiv(other) -> NumericRuby 2.4.0 から[permalink][rdoc][edit]

self を other で割った商を Float で返します。ただし Complex が関わる場合は例外です。その場合も成分は Float になります。

[PARAM] other:
self を割る数を指定します。
654321.fdiv(13731)      # => 47.652829364212366
654321.fdiv(13731.24)   # => 47.65199646936475

-1234567890987654321.fdiv(13731)      # => -89910996357705.52
-1234567890987654321.fdiv(13731.24)   # => -89909424858035.72

[SEE_ALSO] Numeric#quo, Numeric#div, Integer#div

floor(ndigits = 0) -> Integer[permalink][rdoc][edit]

self と等しいかより小さな整数のうち最大のものを返します。

[PARAM] ndigits:
10進数での小数点以下の有効桁数を整数で指定します。負の整数を指定した場合、小数点位置から左に少なくとも n 個の 0 が並びます。
p 1.floor         # => 1
p 1.floor(2)      # => 1
p 18.floor(-1)    # => 10
p (-18).floor(-1) # => -20

[SEE_ALSO] Numeric#floor

gcd(n) -> Integer[permalink][rdoc][edit]

自身と整数 n の最大公約数を返します。

[EXCEPTION] ArgumentError:
n に整数以外のものを指定すると発生します。
p 2.gcd(2)                  # => 2
p 3.gcd(7)                  # => 1
p 3.gcd(-7)                 # => 1
p ((1<<31)-1).gcd((1<<61)-1)  # => 1

また、self や n が 0 だった場合は、0 ではない方の整数の絶対値を返します。

p 3.gcd(0)                  # => 3
p 0.gcd(-7)                 # => 7

[SEE_ALSO] Integer#lcm, Integer#gcdlcm

gcdlcm(n) -> [Integer][permalink][rdoc][edit]

自身と整数 n の最大公約数と最小公倍数の配列 [self.gcd(n), self.lcm(n)] を返します。

[EXCEPTION] ArgumentError:
n に整数以外のものを指定すると発生します。
p 2.gcdlcm(2)                  # => [2, 2]
p 3.gcdlcm(-7)                 # => [1, 21]
p ((1<<31)-1).gcdlcm((1<<61)-1)  # => [1, 4951760154835678088235319297]

[SEE_ALSO] Integer#gcd, Integer#lcm

to_s(base=10) -> String[permalink][rdoc][edit]
inspect(base=10) -> StringRuby 2.4.0 から

整数を 10 進文字列表現に変換します。

引数を指定すれば、それを基数とした文字列表現に変換します。

p 10.to_s(2)    # => "1010"
p 10.to_s(8)    # => "12"
p 10.to_s(16)   # => "a"
p 35.to_s(36)   # => "z"
[RETURN]
数値の文字列表現
[PARAM] base:
基数となる 2 - 36 の数値。
[EXCEPTION] ArgumentError:
base に 2 - 36 以外の数値を指定した場合に発生します。
integer? -> true[permalink][rdoc][edit]

常に真を返します。

p 1.integer?   # => true
p 1.0.integer? # => false
lcm(n) -> Integer[permalink][rdoc][edit]

自身と整数 n の最小公倍数を返します。

[EXCEPTION] ArgumentError:
n に整数以外のものを指定すると発生します。
p 2.lcm(2)                  # => 2
p 3.lcm(-7)                 # => 21
p ((1<<31)-1).lcm((1<<61)-1)  # => 4951760154835678088235319297

また、self や n が 0 だった場合は、0 を返します。

p 3.lcm(0)                  # => 0
p 0.lcm(-7)                 # => 0

[SEE_ALSO] Integer#gcd, Integer#gcdlcm

next -> Integer[permalink][rdoc][edit]
succ -> Integer

self の次の整数を返します。

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

[SEE_ALSO] Integer#pred

nobits?(mask) -> boolRuby 2.5.0 から[permalink][rdoc][edit]

self & mask のすべてのビットが 0 なら true を返します。

self & mask == 0 と等価です。

[PARAM] mask:
ビットマスクを整数で指定します。
p 42.nobits?(42)                 # => false
p 0b1010_1010.nobits?(0b1000_0010) # => false
p 0b1010_1010.nobits?(0b1000_0001) # => false
p 0b0100_0101.nobits?(0b1010_1010) # => true

[SEE_ALSO] Integer#allbits?

[SEE_ALSO] Integer#anybits?

numerator -> Integer[permalink][rdoc][edit]

分子(常に自身)を返します。

[RETURN]
分子を返します。
p 10.numerator  # => 10
p -10.numerator # => -10

[SEE_ALSO] Integer#denominator

odd? -> bool[permalink][rdoc][edit]

自身が奇数であれば真を返します。そうでない場合は偽を返します。

p 5.odd?   # => true
p 10.odd?  # => false
ord -> Integer[permalink][rdoc][edit]

自身を返します。

p 10.ord  #=> 10
# String#ord
p ?a.ord  #=> 97

[SEE_ALSO] String#ord

pred -> Integer[permalink][rdoc][edit]

self から -1 した値を返します。

p 1.pred    #=> 0
p (-1).pred #=> -2

[SEE_ALSO] Integer#next

rationalize -> RationalRuby 1.9.3 から[permalink][rdoc][edit]
rationalize(eps) -> Rational

自身を Rational に変換します。

[PARAM] eps:
許容する誤差

引数 eps は常に無視されます。

p 2.rationalize    # => (2/1)
p 2.rationalize(100) # => (2/1)
p 2.rationalize(0.1) # => (2/1)
remainder(other) -> NumericRuby 2.4.0 から[permalink][rdoc][edit]

self を other で割った余り r を返します。

r の符号は self と同じになります。

[PARAM] other:
self を割る数。
p 5.remainder(3)  # =>  2
p -5.remainder(3) # => -2
p 5.remainder(-3) # =>  2
p -5.remainder(-3)  # => -2

p -1234567890987654321.remainder(13731)    # => -6966
p -1234567890987654321.remainder(13731.24) # => -9906.22531493148

[SEE_ALSO] Integer#divmod, Integer#modulo, Numeric#modulo

round(ndigits = 0, half: :up) -> Integer[permalink][rdoc][edit]

self ともっとも近い整数を返します。

[PARAM] ndigits:
10進数での小数点以下の有効桁数を整数で指定します。負の整数を指定した場合、小数点位置から左に少なくとも n 個の 0 が並びます。
[PARAM] half:
ちょうど半分の値の丸め方を指定します。サポートされている値は以下の通りです。
  • :up or nil: 0から遠い方に丸められます。
  • :even: もっとも近い偶数に丸められます。
  • :down: 0に近い方に丸められます。
p 1.round       # => 1
p 1.round(2)    # => 1
p 15.round(-1)  # =>  20
p (-15).round(-1) # => -20

p 25.round(-1, half: :up)    # => 30
p 25.round(-1, half: :down)  # => 20
p 25.round(-1, half: :even)  # => 20
p 35.round(-1, half: :up)    # => 40
p 35.round(-1, half: :down)  # => 30
p 35.round(-1, half: :even)  # => 40
p (-25).round(-1, half: :up) # => -30
p (-25).round(-1, half: :down) # => -20
p (-25).round(-1, half: :even) # => -20

[SEE_ALSO] Numeric#round

size -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]

整数の実装上のサイズをバイト数で返します。

p 1.size              # => 8
p 0x1_0000_0000.size  # => 8

[SEE_ALSO] Integer#bit_length

times {|n| ... } -> self[permalink][rdoc][edit]
times -> Enumerator

self 回だけ繰り返します。 self が正の整数でない場合は何もしません。

またブロックパラメータには 0 から self - 1 までの数値が渡されます。

3.times { puts "Hello, World!" }  # Hello, World! と3行続いて表示される。
0.times { puts "Hello, World!" }  # 何も表示されない。
5.times {|n| print n }            # 01234 と表示される。

[SEE_ALSO] Integer#upto, Integer#downto, Numeric#step

to_f -> FloatRuby 2.4.0 から[permalink][rdoc][edit]

self を浮動小数点数(Float)に変換します。

self が Float の範囲に収まらない場合、Float::INFINITY を返します。

p 1.to_f                     # => 1.0
p (Float::MAX.to_i * 2).to_f # => Infinity
p (-Float::MAX.to_i * 2).to_f  # => -Infinity
to_i -> self[permalink][rdoc][edit]
to_int -> self

self を返します。

p 10.to_i # => 10
to_r -> Rational[permalink][rdoc][edit]

自身を Rational に変換します。

p 1.to_r      # => (1/1)
p (1<<64).to_r  # => (18446744073709551616/1)
truncate(ndigits = 0) -> Integer[permalink][rdoc][edit]

0 から self までの整数で、自身にもっとも近い整数を返します。

[PARAM] ndigits:
10進数での小数点以下の有効桁数を整数で指定します。負の整数を指定した場合、小数点位置から左に少なくとも n 個の 0 が並びます。
p 1.truncate         # => 1
p 1.truncate(2)      # => 1
p 18.truncate(-1)    # =>  10
p (-18).truncate(-1) # => -10

[SEE_ALSO] Numeric#truncate

upto(max) {|n| ... } -> Integer[permalink][rdoc][edit]
upto(max) -> Enumerator

self から max まで 1 ずつ増やしながら繰り返します。 self > max であれば何もしません。

[PARAM] max:
数値
[RETURN]
self を返します。
p 5.upto(10) {|i| print i, " " } # => 5 6 7 8 9 10

[SEE_ALSO] Integer#downto, Numeric#step, Integer#times

self | other -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]

ビット二項演算子。論理和を計算します。

[PARAM] other:
数値
p 1 | 1  # => 1
p 2 | 3  # => 3
~ self -> IntegerRuby 2.4.0 から[permalink][rdoc][edit]

ビット演算子。否定を計算します。

p ~1  # => -2
p ~3  # => -4
p ~-4 # => 3

追加されるメソッド

each_prime(upper_bound) {|prime| ... } -> objectRuby 1.9.3 から[permalink][rdoc][edit] [added by prime]
each_prime(upper_bound) -> Enumerator [added by prime]

全ての素数を列挙し、それぞれの素数をブロックに渡して評価します。

[PARAM] upper_bound:
任意の正の整数を指定します。列挙の上界です。 nil が与えられた場合は無限に列挙し続けます。
[RETURN]
ブロックの最後に評価された値を返します。ブロックが与えられなかった場合は、Enumerator と互換性のある外部イテレータを返します。

[SEE_ALSO] Prime#each

from_prime_division(pd) -> Integer[permalink][rdoc][edit] [added by prime]

素因数分解された結果を元の数値に戻します。

[PARAM] pd:
整数のペアの配列を指定します。含まれているペアの第一要素は素因数を、第二要素はその素因数の指数をあらわします。

[SEE_ALSO] Prime#int_from_prime_division

require 'prime'
p Prime.int_from_prime_division([[2,2], [3,1]])  #=> 12
p Prime.int_from_prime_division([[2,2], [3,2]])  #=> 36
prime? -> boolRuby 1.9.3 から[permalink][rdoc][edit] [added by prime]

自身が素数である場合、真を返します。そうでない場合は偽を返します。

require 'prime'
p 1.prime? # => false
p 2.prime? # => true

[SEE_ALSO] Prime#prime?

prime_division(generator = Prime::Generator23.new) -> [[Integer, Integer]][permalink][rdoc][edit] [added by prime]

自身を素因数分解した結果を返します。

[PARAM] generator:
素数生成器のインスタンスを指定します。
[RETURN]
素因数とその指数から成るペアを要素とする配列です。つまり、戻り値の各要素は2要素の配列 [n,e] であり、それぞれの内部配列の第1要素 n は self の素因数、第2要素は n**e が self を割り切る最大の自然数 e です。
[EXCEPTION] ZeroDivisionError:
self がゼロである場合に発生します。

[SEE_ALSO] Prime#prime_division

require 'prime'
p 12.prime_division #=> [[2,2], [3,1]]
p 10.prime_division #=> [[2,1], [5,1]]
to_bn -> OpenSSL::BN[permalink][rdoc][edit] [added by openssl]

Integer を同じ数を表す OpenSSL::BN のオブジェクトに変換します。

require 'openssl'

pp 5.to_bn     #=> #<OpenSSL::BN 5>
pp (-5).to_bn  #=> #<OpenSSL::BN -5>

なお、実装は、以下のようになっています。

class Integer
  def to_bn
    OpenSSL::BN.new(self)
  end
end

[SEE_ALSO] OpenSSL::BN.new, OpenSSL::BN#to_i

to_d -> BigDecimalRuby 1.9.3 から[permalink][rdoc][edit] [added by bigdecimal/util]

自身を BigDecimal に変換します。BigDecimal(self) と同じです。

[RETURN]
BigDecimal に変換したオブジェクト