66件ヒット
[1-66件を表示]
(0.083秒)
検索結果
先頭5件
-
Rational (46072.0)
-
有理数を扱うクラスです。
...様に Rational.new ではなく、 Kernel.#Rational を使用して
Rational オブジェクトを作成します。
//emlist[例][ruby]{
Rational(1, 3) # => (1/3)
Rational('1/3') # => (1/3)
Rational('0.33') # => (33/100)
Rational.new(1, 3) # => NoMethodError
//}
Rational オブ......ジェクトは常に既約(それ以上約分できない状態)である
事に注意してください。
//emlist[例][ruby]{
Rational(2, 6) # => (1/3)
Rational(1, 3) * 3 # => (1/1)
//}... -
Kernel
. # Rational(x , y = 1) -> Rational (26392.0) -
引数を有理数(Rational)に変換した結果を返します。
...引数を有理数(Rational)に変換した結果を返します。
@param x 変換対象のオブジェクトです。
@param y 変換対象のオブジェクトです。省略した場合は x だけを用いて
Rational オブジェクトを作成します。
@raise ArgumentError......x/y した Rational オブジェクトを
返します。
//emlist[例][ruby]{
Rational("1/3") # => (1/3)
Rational(1, 3) # => (1/3)
Rational("0.1", "0.3") # => (1/3)
Rational(Complex(1,2), 2) # => ((1/2)+(1/1)*i)
//}
ただし、1.8系とは異なり、Rational オブジェ......クトは常に既約(それ以上
約分できない状態)である事に注意してください。
//emlist[例][ruby]{
Rational(2, 6) # => (1/3)
Rational(1, 3) * 3 # => (1/1)
//}
引数に文字列を指定する場合、以下のいずれかの形式で指定します。
* "1/3... -
Kernel
. # Rational(x , y = 1 , exception: true) -> Rational | nil (26392.0) -
引数を有理数(Rational)に変換した結果を返します。
...引数を有理数(Rational)に変換した結果を返します。
@param x 変換対象のオブジェクトです。
@param y 変換対象のオブジェクトです。省略した場合は x だけを用いて
Rational オブジェクトを作成します。
@param exception false......x/y した Rational オブジェクトを
返します。
//emlist[例][ruby]{
Rational("1/3") # => (1/3)
Rational(1, 3) # => (1/3)
Rational("0.1", "0.3") # => (1/3)
Rational(Complex(1,2), 2) # => ((1/2)+(1/1)*i)
//}
ただし、1.8系とは異なり、Rational オブジェ......クトは常に既約(それ以上
約分できない状態)である事に注意してください。
//emlist[例][ruby]{
Rational(2, 6) # => (1/3)
Rational(1, 3) * 3 # => (1/1)
//}
引数に文字列を指定する場合、以下のいずれかの形式で指定します。
* "1/3... -
Random
# rand -> Float (8072.0) -
一様な擬似乱数を発生させます。
...が存在しない範囲を渡した時などに発生します。
//emlist[例][ruby]{
# Kernel.#rand とほぼ同様の使い勝手
prng = Random.new(1234)
prng.rand # => 0.1915194503788923
srand(1234)
rand # => 0.1915194503788923
# max に実数も指定出来る
prng......る
prng.rand(Time.new(2012, 1, 1) ... Time.new(2013,1,1)) # => 2012-02-25 03:11:42 +0900
require 'date'
prng.rand(Date.new(2012, 1, 1) ... Date.new(2013,1,1)) # => #<Date: 2012-01-31 ((2455958j,0s,0n),+0s,2299161j)>
# Kernel.#rand とほぼ同様の使い勝手
prng = Random.new(1234)
prng.rand......すため。
prng.rand(Time.new(2012, 1, 1) ... Time.new(2013,1,1)) # => 2012-02-25 03:11:42 +0900
# DateTime ではうまくいかない。というのは DateTime#- は
# Rational を返すため。
# Date#- も Rational を返すが通常は整数を表す Rational オブジェクトを返すた... -
Random
# rand(max) -> Integer | Float (8072.0) -
一様な擬似乱数を発生させます。
...が存在しない範囲を渡した時などに発生します。
//emlist[例][ruby]{
# Kernel.#rand とほぼ同様の使い勝手
prng = Random.new(1234)
prng.rand # => 0.1915194503788923
srand(1234)
rand # => 0.1915194503788923
# max に実数も指定出来る
prng......る
prng.rand(Time.new(2012, 1, 1) ... Time.new(2013,1,1)) # => 2012-02-25 03:11:42 +0900
require 'date'
prng.rand(Date.new(2012, 1, 1) ... Date.new(2013,1,1)) # => #<Date: 2012-01-31 ((2455958j,0s,0n),+0s,2299161j)>
# Kernel.#rand とほぼ同様の使い勝手
prng = Random.new(1234)
prng.rand......すため。
prng.rand(Time.new(2012, 1, 1) ... Time.new(2013,1,1)) # => 2012-02-25 03:11:42 +0900
# DateTime ではうまくいかない。というのは DateTime#- は
# Rational を返すため。
# Date#- も Rational を返すが通常は整数を表す Rational オブジェクトを返すた... -
Random
# rand(range) -> Integer | Float (8072.0) -
一様な擬似乱数を発生させます。
...が存在しない範囲を渡した時などに発生します。
//emlist[例][ruby]{
# Kernel.#rand とほぼ同様の使い勝手
prng = Random.new(1234)
prng.rand # => 0.1915194503788923
srand(1234)
rand # => 0.1915194503788923
# max に実数も指定出来る
prng......る
prng.rand(Time.new(2012, 1, 1) ... Time.new(2013,1,1)) # => 2012-02-25 03:11:42 +0900
require 'date'
prng.rand(Date.new(2012, 1, 1) ... Date.new(2013,1,1)) # => #<Date: 2012-01-31 ((2455958j,0s,0n),+0s,2299161j)>
# Kernel.#rand とほぼ同様の使い勝手
prng = Random.new(1234)
prng.rand......すため。
prng.rand(Time.new(2012, 1, 1) ... Time.new(2013,1,1)) # => 2012-02-25 03:11:42 +0900
# DateTime ではうまくいかない。というのは DateTime#- は
# Rational を返すため。
# Date#- も Rational を返すが通常は整数を表す Rational オブジェクトを返すた... -
Numeric
# coerce(other) -> [Numeric] (8042.0) -
自身と other が同じクラスになるよう、自身か other を変換し [other, self] という配列にして返します。
...義しなければなりません。
以下は Rational の coerce のソースです。other が自身の知らない数値クラスであった場合、
super を呼んでいることに注意して下さい。
//emlist[例][ruby]{
# lib/rational.rb より
def coerce(other)
if other.kind_of?(......turn Rational.new!(other, 1), self
else
super
end
end
//}
数値クラスの算術演算子は通常自分と演算できないクラスをオペランドとして受け
取ると coerce を使って自分とオペランドを変換した上で演算を行います。
以下は Rational の......、引数の coerce により自身を変換してから
+ 演算子を呼んでいます。
//emlist[例][ruby]{
# lib/rational.rb より
def + (a)
if a.kind_of?(Rational)
# 長いので省略
elsif a.kind_of?(Integer)
# 長いので省略
elsif a.kind_of?(Float)
Float(self) + a...