Ruby 2.5.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Integerクラス

class Integer

クラス・モジュールの継承リスト: Integer < Numeric < Comparable < Object < Kernel < BasicObject
aliases: Fixnum, Bignum
dynamic include: JSON::Generator::GeneratorMethods::Integer (by json)

要約

整数クラスです。

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

2.4.0 から Fixnum, Bignum は Integerに統合されました。 2.4.0 からはどちらも Integer クラスのエイリアスとなっています。

特異メソッド

定義 説明
sqrt(n) -> Integer

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

インスタンスメソッド

定義 説明
self % other -> Numeric
modulo(other) -> Numeric

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

self & other -> Integer

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

self * other -> Numeric

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

self ** other -> Numeric
pow(other) -> Numeric
pow(other, modulo) -> Integer

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

self + other -> Numeric

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

self - other -> Numeric

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

- self -> Integer

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

self / other -> Numeric

除算の算術演算子。

self < other -> bool

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

self << bits -> Integer

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

self <= other -> bool

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

self <=> other -> -1 | 0 | 1 | nil

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

self == other -> bool
self === other -> bool

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

self > other -> bool

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

self >= other -> bool

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

self >> bits -> Integer

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

self[nth] -> Integer

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

self ^ other -> Integer

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

abs -> Integer
magnitude -> Integer

self の絶対値を返します。

allbits?(mask) -> bool

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

anybits?(mask) -> bool

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

bit_length -> Integer

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

ceil(ndigits = 0) -> Integer

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

chr -> String
chr(encoding) -> String

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

denominator -> Integer

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

digits -> [Integer]
digits(base) -> [Integer]

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

div(other) -> Integer

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

divmod(other) -> [Integer, Numeric]

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

downto(min) {|n| ... } -> self
downto(min) -> Enumerator

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

even? -> bool

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

fdiv(other) -> Numeric

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

floor(ndigits = 0) -> Integer

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

gcd(n) -> Integer

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

gcdlcm(n) -> [Integer]

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

to_s(base=10) -> String
inspect(base=10) -> String

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

integer? -> true

常に真を返します。

lcm(n) -> Integer

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

next -> Integer
succ -> Integer

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

nobits?(mask) -> bool

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

numerator -> Integer

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

odd? -> bool

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

ord -> Integer

自身を返します。

pred -> Integer

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

rationalize -> Rational
rationalize(eps) -> Rational

自身を Rational に変換します。

remainder(other) -> Numeric

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

round(ndigits = 0, half: :up) -> Integer

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

size -> Integer

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

times {|n| ... } -> self
times -> Enumerator

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

to_f -> Float

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

to_i -> self
to_int -> self

self を返します。

to_r -> Rational

自身を Rational に変換します。

truncate(ndigits = 0) -> Integer

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

upto(max) {|n| ... } -> Integer
upto(max) -> Enumerator

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

self | other -> Integer

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

~ self -> Integer

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

継承したメソッド

! != __id__ __send__ instance_eval instance_exec method_missing singleton_method_added singleton_method_removed singleton_method_undefined +@ abs2 angle coerce conj eql? finite? i imag infinite? negative? nonzero? polar positive? quo real real? rect step to_c zero? !~ =~ _dump class clone define_singleton_method display enum_for equal? extend freeze frozen? hash initialize initialize_copy instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? itself marshal_dump marshal_load method methods nil? object_id pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods psych_to_yaml public_method public_methods public_send remove_instance_variable respond_to? respond_to_missing? send singleton_class singleton_method singleton_methods taint tainted? tap to_a to_ary to_hash to_io to_proc to_regexp to_str trust untaint untrust untrusted? yield_self .new .yaml_tag ::ARGF ::ARGV ::DATA ::ENV ::FALSE ::NIL ::RUBY_COPYRIGHT ::RUBY_DESCRIPTION ::RUBY_ENGINE ::RUBY_ENGINE_VERSION ::RUBY_PATCHLEVEL ::RUBY_PLATFORM ::RUBY_RELEASE_DATE ::RUBY_REVISION ::RUBY_VERSION ::SCRIPT_LINES__ ::STDERR ::STDIN ::STDOUT ::TOPLEVEL_BINDING ::TRUE

追加されるメソッド

定義 説明 ライブラリ
each_prime(upper_bound) {|prime| ... } -> object
each_prime(upper_bound) -> Enumerator

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

prime
from_prime_division(pd) -> Integer

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

prime
prime? -> bool

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

prime
prime_division(generator = Prime::Generator23.new) -> [[Integer, Integer]]

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

prime
to_bn -> OpenSSL::BN

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

openssl
to_d -> BigDecimal

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

bigdecimal/util