るりまサーチ

最速Rubyリファレンスマニュアル検索!
722件ヒット [301-400件を表示] (0.154秒)
トップページ > クエリ:t[x] > クエリ:Ruby[x] > クエリ:ruby[x] > クエリ:@[x] > 種類:インスタンスメソッド[x] > クラス:Integer[x]

別のキーワード

  1. rbconfig ruby
  2. fiddle ruby_free
  3. fiddle build_ruby_platform
  4. rake ruby
  5. rubygems/defaults ruby_engine

ライブラリ

キーワード

検索結果

<< < ... 2 3 4 5 6 ... > >>

Integer#pow(other) -> Numeric (3162.0)

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

...乗)を計算します。

@
param other 二項演算の右側の引数(対象)
@
param modulo 指定すると、計算途中に巨大な値を生成せずに (self**other) % modulo と同じ結果を返します。
@
return 計算結果
@
raise TypeError 2引数 pow で Integer 以外を指定した場...
...合に発生します。
@raise RangeError 2引数 pow で other に負の数を指定した場合に発生します。

//emlist[][ruby]{
2 ** 3 # => 8
2 ** 0 # => 1
0 ** 0 # => 1
3.pow(3, 8) # => 3
3.pow(3, -8) # => -5
3.pow(2, -2) # => -1
-3.pow(3, 8) # => 5
-3.pow(3, -8) # => -3
5.pow(2,...
...になりそうなとき、警告を出したうえで Float::INFINITY を返します。

//emlist[計算を放棄して Float::INFINITY を返す例][ruby]{
p 100**9999999
# => warning: in a**b, b may be too big
# Infinity
//}

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


@
see BigDecimal#power...

Integer#pow(other, modulo) -> Integer (3162.0)

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

...乗)を計算します。

@
param other 二項演算の右側の引数(対象)
@
param modulo 指定すると、計算途中に巨大な値を生成せずに (self**other) % modulo と同じ結果を返します。
@
return 計算結果
@
raise TypeError 2引数 pow で Integer 以外を指定した場...
...合に発生します。
@raise RangeError 2引数 pow で other に負の数を指定した場合に発生します。

//emlist[][ruby]{
2 ** 3 # => 8
2 ** 0 # => 1
0 ** 0 # => 1
3.pow(3, 8) # => 3
3.pow(3, -8) # => -5
3.pow(2, -2) # => -1
-3.pow(3, 8) # => 5
-3.pow(3, -8) # => -3
5.pow(2,...
...になりそうなとき、警告を出したうえで Float::INFINITY を返します。

//emlist[計算を放棄して Float::INFINITY を返す例][ruby]{
p 100**9999999
# => warning: in a**b, b may be too big
# Infinity
//}

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


@
see BigDecimal#power...

Integer#chr -> String (3150.0)

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

...//emlist[][ruby]{
p 65.chr
# => "A"
p 12354.chr
# => `chr': 12354 out of char range (RangeError)

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

引数無しで呼ばれた場合は self を US-ASCII、ASCII-8BIT、デ...
...ます。

//emlist[][ruby]{
p 0x79.chr.encoding # => #<Encoding:US_ASCII>
p 0x80.chr.encoding # => #<Encoding:ASCII_8BIT>
//}

@
param encoding エンコーディングを表すオブジェクト。Encoding::UTF_8、'shift_jis' など。
@
return 一文字からなる文字列
@
raise RangeError s...
...elf を与えられたエンコーディングで正しく解釈できない場合に発生します。
@
see String#ord Encoding.default_internal...

Integer#chr(encoding) -> String (3150.0)

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

...//emlist[][ruby]{
p 65.chr
# => "A"
p 12354.chr
# => `chr': 12354 out of char range (RangeError)

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

引数無しで呼ばれた場合は self を US-ASCII、ASCII-8BIT、デ...
...ます。

//emlist[][ruby]{
p 0x79.chr.encoding # => #<Encoding:US_ASCII>
p 0x80.chr.encoding # => #<Encoding:ASCII_8BIT>
//}

@
param encoding エンコーディングを表すオブジェクト。Encoding::UTF_8、'shift_jis' など。
@
return 一文字からなる文字列
@
raise RangeError s...
...elf を与えられたエンコーディングで正しく解釈できない場合に発生します。
@
see String#ord Encoding.default_internal...

Integer#[](nth) -> Integer (3138.0)

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

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

@
param nth 何ビット目を指すかの数値
@
return 1 か 0

//emlist[][ruby]{
a = 0b11001100101010
30.downto(0) {|n| print a[n] }
# => 00000000000000000110...
...01100101010

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

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

//emlist[][ruby]{
p 255[-1] # => 0
//}


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

絞り込み条件を変える

Integer#gcd(n) -> Integer (3138.0)

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

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

@
raise ArgumentError n に整数以外のものを指定すると発生します。

//emlist[][ruby]{
2.gcd(2) # => 2
3.gcd(7) # => 1
3.gcd(-7) # => 1
((1<<31)-1).gcd((1<<61)-1) # => 1...
...//}

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

//emlist[][ruby]{
3.gcd(0) # => 3
0.gcd(-7) # => 7
//}

@
see Integer#lcm, Integer#gcdlcm...

Integer#lcm(n) -> Integer (3138.0)

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

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

@
raise ArgumentError n に整数以外のものを指定すると発生します。

//emlist[][ruby]{
2.lcm(2) # => 2
3.lcm(-7) # => 21
((1<<31)-1).lcm((1<<61)-1) # => 4951760154835678088235319297
//}...
...また、self や n が 0 だった場合は、0 を返します。

//emlist[][ruby]{
3.lcm(0) # => 0
0.lcm(-7) # => 0
//}

@
see Integer#gcd, Integer#gcdlcm...

Integer#/(other) -> Numeric (3132.0)

除算の算術演算子。

...算術演算子。

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

other が Float、Rational、Complex の場合、普通の商を other と
同じクラ...
...

@
param other 二項演算の右側の引数(対象)
@
return 計算結果

//emlist[例][ruby]{
7 / 2 # => 3
7 / -2 # => -4
7 / 2.0 # => 3.5
7 / Rational(2, 1) # => (7/2)
7 / Complex(2, 0) # => ((7/2)+0i)

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

@
see Integer#d...
...iv, Integer#fdiv, Numeric#quo...

Integer#div(other) -> Integer (3132.0)

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

...って整商とします。

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

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

@
param other 二項演算の右側の引数(対象)
@
return 計算結果

//emlist[例][ruby]{
7.div(2) # => 3
7.div(-2) #...
...(Rational(2, 1)) # => 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 Integer#fdiv, Integer#/...
..., Integer#modulo...

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

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

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

@
param ndigits 10進数での小数点以下の有効桁数を整数で指定します。
負の整数を指定した場合、小数点位置から左に少なくとも n 個の 0 が並びます。
@
param half ちょうど半分の値の丸...
...い方に丸められます。
* :even: もっとも近い偶数に丸められます。
* :down: 0に近い方に丸められます。

//emlist[][ruby]{
1.round # => 1
1.round(2) # => 1
15.round(-1) # => 20
(-15).round(-1) # => -20

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

@
see Numeric#round...

絞り込み条件を変える

<< < ... 2 3 4 5 6 ... > >>