要約
整数の抽象クラス。サブクラスとして Fixnum と Bignum があります。この 2 種類の整数は値の大きさに応じてお互いに自動的に変換されます。ビット操作において整数は無限の長さのビットストリングとみなすことができます。
目次
- 特異メソッド
- インスタンスメソッド
- 追加されるメソッド
- 再定義されたメソッド
継承しているメソッド
- Precisionから継承しているメソッド
- Numericから継承しているメソッド
- Comparableから継承しているメソッド
特異メソッド
induced_from(num) -> Integer[permalink][rdoc]-
num を Integer に変換した結果を返します。引数が数値の場合は小数部が切り捨てられ、そうでない場合はTypeErrorをraiseします。
Integer.induced_from 1.4 #=> 1 Integer.induced_from 1.9 #=> 1 Integer.induced_from -1.9 #=> -1 Integer.induced_from '1' #=> TypeError
include している Precision モジュールの Precision.induced_from を上書きしています。
- [PARAM] num:
- 数値
- [RETURN]
- 数値
インスタンスメソッド
chr -> String[permalink][rdoc]-
文字コードに対応する 1 バイトの文字列を返します。
例えば65.chr は "A" を返します。
逆に文字列から文字コードを得るには "A"[0] とします
- [RETURN]
- 1バイト文字列
- [EXCEPTION] RangeError:
- self が 0 から 255 の範囲外の場合に発生します。
[SEE_ALSO] String#[]
downto(min) {|n| ... } -> self[permalink][rdoc]downto(min) -> Enumerable::Enumerator-
self から min まで 1 ずつ減らしながらブロックを繰り返し実行します。 self < min であれば何もしません。
- [PARAM] min:
- 数値
- [RETURN]
- self を返します。
[SEE_ALSO] Integer#upto, Numeric#step, Integer#times
even? -> bool[permalink][rdoc]-
自身が偶数であれば真を返します。そうでない場合は偽を返します。
integer? -> true[permalink][rdoc]-
常に真を返します。
next -> Fixnum | Bignum[permalink][rdoc]succ -> Fixnum | Bignum-
self の次の整数を返します。
odd? -> bool[permalink][rdoc]-
自身が奇数であれば真を返します。そうでない場合は偽を返します。
ord -> Integer[permalink][rdoc]-
自身を返します。
10.ord #=> 10 ?a.ord #=> 97
pred -> Integer[permalink][rdoc]-
self から -1 した値を返します。
1.pred #=> 0 (-1).pred #=> -2
times {|n| ... } -> self[permalink][rdoc]times -> Enumerable::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_i -> self[permalink][rdoc]to_int -> self-
self を返します。
to_s -> String[permalink][rdoc]to_s(base) -> String-
整数を 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 以外の数値を指定した場合に発生します。
upto(max) {|n| ... } -> Fixnum | Bignum[permalink][rdoc]upto(max) -> Enumerable::Enumerator-
self から max まで 1 ずつ増やしながら繰り返します。 self > max であれば何もしません。
- [PARAM] max:
- 数値
- [RETURN]
- self を返します。
[SEE_ALSO] Integer#downto, Numeric#step, Integer#times
追加されるメソッド
denominator -> Integer[permalink][rdoc] [added by rational]-
分母(常に1)を返します。
- [RETURN]
- 分母を返します。
[SEE_ALSO] Integer#numerator
from_prime_division(pd) -> Integer[permalink][rdoc] [added by mathn]-
素因数分解の配列 pd から数を求めます。 pd は [素因数, 指数] 組の配列です。
例:
Integer.from_prime_division [[2,3],[3,2]] # => 72 == 2**3 * 3**2
gcd(n) -> Integer[permalink][rdoc] [added by rational]-
自身と整数 n の最大公約数を返します。
- [PARAM] n:
- 自身との最大公約数を計算する数
2.gcd(2) # => 2 3.gcd(7) # => 1 3.gcd(-7) # => 1 ((1<<31)-1).gcd((1<<61)-1) # => 1
また、self や n が 0 だった場合は、0 ではない方の整数の絶対値を返します。
3.gcd(0) # => 3 0.gcd(-7) # => 7
[SEE_ALSO] Integer#lcm, Integer#gcdlcm
gcd2(int)[permalink][rdoc] [added by mathn]-
[TODO]
例:
12.gcd2 8 # => 4
gcdlcm(n) -> [Integer][permalink][rdoc] [added by rational]-
自身と整数 n の最大公約数と最小公倍数の配列 [self.gcd(n), self.lcm(n)] を返します。
- [PARAM] n:
- 自身との最大公約数と最小公倍数を計算する数
- [EXCEPTION] ArgumentError:
- n に整数以外のものを指定すると発生します。
例:
6.gcdlcm(9) # => [3, 18] 2.gcdlcm(2) # => [2, 2] 3.gcdlcm(-7) # => [1, 21] ((1<<31)-1).gcdlcm((1<<61)-1) # => [1, 4951760154835678088235319297]
[SEE_ALSO] Integer#gcd, Integer#lcm
lcm(n) -> Integer[permalink][rdoc] [added by rational]-
自身と整数 n の最小公倍数を返します。
- [PARAM] n:
- 自身との最小公倍数を計算する数
例:
2.lcm(2) # => 2 3.lcm(-7) # => 21 ((1<<31)-1).lcm((1<<61)-1) # => 4951760154835678088235319297
また、self や n が 0 だった場合は、0 を返します。
3.lcm(0) # => 0 0.lcm(-7) # => 0
[SEE_ALSO] Integer#gcd, Integer#gcdlcm
numerator -> Integer[permalink][rdoc] [added by rational]-
分子(常に自身)を返します。
- [RETURN]
- 分子を返します。
[SEE_ALSO] Integer#denominator
power!(other) -> Integer | Float[permalink][rdoc] [added by rational]-
冪(べき)乗を計算します。
- [PARAM] other:
- 自身を other 乗する数
rationalで再定義される前のInteger#**の別名です。 other が正または 0 の整数 (Integer) ならば、整数 (Integer) を、それ以外なら、浮動小数 (Float) を返します。
prime_division[permalink][rdoc] [added by mathn]-
[TODO]
各素因子について素因子と指数の組を並べた配列を返します。
例:
72.prime_division # => [[2, 3], [3, 2]]
taguri -> String[permalink][rdoc] [added by yaml/rubytypes]-
自身のタグ URI を返します。
taguri=(val)[permalink][rdoc] [added by yaml/rubytypes]-
自身のタグ URI を val に設定します。
- [PARAM] val:
- タグ URI を文字列で指定します。
to_bn -> OpenSSL::BN[permalink][rdoc] [added by openssl]-
Integer を同じ数を表す OpenSSL::BN のオブジェクトに変換します。
現在のバージョンでは正しく実装されていません。
to_r -> Rational[permalink][rdoc] [added by rational]-
自身を Rational に変換します。
例:
1.to_r # => Rational(1, 1) (1<<64).to_r # => Rational(18446744073709551616, 1)
to_yaml(opts = {}) -> String[permalink][rdoc] [added by yaml/rubytypes]-
自身を YAML ドキュメントに変換します。
- [PARAM] opts:
- YAML ドキュメント出力の際のオプションを指定します。オプションの詳細は YAML::Syck::Emitter#reset を参照してください。
print 1.to_yaml # => --- 1 print -1.to_yaml # => --- -1
yaml_tag_subclasses? -> true[permalink][rdoc] [added by yaml/rubytypes]-
常に true を返します。
ライブラリ内部で使用します。
再定義されたメソッド
self ** other -> Rational | Float | Integer[permalink][rdoc] [redefined by rational]-
冪(べき)乗を計算します。
- [PARAM] other:
- 自身を other 乗する数
計算結果は以下のようになります。
- otherが正または0の整数(Integer)ならば、整数(Integer)を返す。
- otherが負の整数(Integer)ならば、有理数(Rational)を返す。
- otherが有理数(Rational)や浮動小数(Float)ならば、浮動小数(Float)を返す。
例:
2 ** 3 #=> 8 2 ** -3 #=> Rational(1, 8) 2 ** Rational(3) #=> 8.0 2 ** Rational(1, 2) #=> 1.4142135623731
self / other -> Rational | Float | Integer[permalink][rdoc] [redefined by rational]-
商を計算します。
- [PARAM] other:
- 自身を割る数
計算結果は以下のようになります。
- otherが有理数(Rational)ならば、有理数(Rational)を返す。
- otherがそれ以外なら、Integer#/と同じ。つまり、 other が整数(Integer)ならば、整数(Integer)を(整除)、浮動小数(Float)ならば、 浮動小数(Float)を返す。 ただし、いずれも、other == 0 の時は、ZeroDivisionErrorとなる。
- [EXCEPTION] ZeroDivisionError:
- other が 0 の時に発生します。