Ruby 1.9.2 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Integerクラス
クラスの継承リスト: Integer < Numeric < Comparable < Object < Kernel < BasicObject
整数の抽象クラス。サブクラスとして Fixnum と Bignum があり ます。この 2 種類の整数は値の大きさに応じてお互いに自動的に変換されま す。ビット操作において整数は無限の長さのビットストリングとみなすことが できます。
chr -> String文字コードに対応する 1 バイトの文字列を返します。
例えば65.chr は "A" を返します。
逆に1文字の文字列から文字コードを得るには "A".ord とします。
[SEE_ALSO] String#ord
denominator -> Integer分母(常に1)を返します。
[SEE_ALSO] Integer#numerator
downto(min) {|n| ... } -> selfdownto(min) -> Enumeratorself から min まで 1 ずつ減らしながらブロックを繰り返し実行します。 self < min であれば何もしません。
[SEE_ALSO] Integer#upto, Numeric#step, Integer#times
even? -> bool自身が偶数であれば真を返します。 そうでない場合は偽を返します。
gcd(n) -> Integer自身と整数 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
gcdlcm(n) -> [Integer]自身と整数 n の最大公約数と最小公倍数の配列 [self.gcd(n), self.lcm(n)] を返します。
例:
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
integer? -> true常に真を返します。
lcm(n) -> Integer自身と整数 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
next -> Fixnum | Bignumsucc -> Fixnum | Bignumself の次の整数を返します。
numerator -> Integer分子(常に自身)を返します。
[SEE_ALSO] Integer#denominator
odd? -> bool自身が奇数であれば真を返します。 そうでない場合は偽を返します。
ord -> Integer自身を返します。
10.ord #=> 10 # String#ord ?a.ord #=> 97
[SEE_ALSO] String#ord
pred -> Integerself から -1 した値を返します。
1.pred #=> 0 (-1).pred #=> -2
rationalize -> Rationalrationalize(eps) -> Rational自身を Rational に変換します。
引数 eps は常に無視されます。
例:
2.rationalize # => (2/1) 2.rationalize(100) # => (2/1) 2.rationalize(0.1) # => (2/1)
times {|n| ... } -> selftimes -> Enumeratorself 回だけ繰り返します。 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 -> selfto_int -> selfself を返します。
to_r -> Rational自身を Rational に変換します。
例:
1.to_r # => (1/1) (1<<64).to_r # => (18446744073709551616/1)
to_s -> Stringto_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"
upto(max) {|n| ... } -> Fixnum | Bignumupto(max) -> Enumeratorself から max まで 1 ずつ増やしながら繰り返します。 self > max であれば何もしません。
[SEE_ALSO] Integer#downto, Numeric#step, Integer#times