22件ヒット
[1-22件を表示]
(0.195秒)
検索結果
-
Matrix
# coerce(other) -> Array (27237.0) -
他の数値オブジェクトとの変換を行います。
...をMatrix::Scalarのオブジェクトに変換し、selfとの組を配列として返します。
@param other 変換する数値オブジェクト
//emlist[例][ruby]{
require 'matrix'
a1 = [1, 2]
a2 = [-1.25, 2.2]
m = Matrix[a1, a2]
r = Rational(1, 2)
p m.coerce(r) #=> [#<Matrix::Scalar:0x832d......f18 @value=(1/2)>, Matrix[[1, 2], [-1.25, 2.2]]]
//}... -
Numeric
# coerce(other) -> [Numeric] (24373.0) -
自身と other が同じクラスになるよう、自身か other を変換し [other, self] という配列にして返します。
...デフォルトでは self と other を Float に変換して [other, self] という配列にして返します。
Numeric のサブクラスは、このメソッドを適切に再定義しなければなりません。
以下は Rational の coerce のソースです。other が自身の知ら......super を呼んでいることに注意して下さい。
//emlist[例][ruby]{
# lib/rational.rb より
def coerce(other)
if other.kind_of?(Float)
return other, self.to_f
elsif other.kind_of?(Integer)
return Rational.new!(other, 1), self
else
super
end
end
//}
数値クラ......ます。
//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
else
x, y = a.coerce(self)
x + y
end
end
//}
@param other オペラ...