るりまサーチ

最速Rubyリファレンスマニュアル検索!
12件ヒット [1-12件を表示] (0.037秒)
トップページ > クエリ:param[x] > クエリ:end[x] > クラス:Numeric[x] > クエリ:coerce[x]

別のキーワード

  1. net/imap param
  2. win32ole win32ole_param
  3. win32ole_param new
  4. bodytypetext param
  5. bodytypebasic param

ライブラリ

検索結果

Numeric#coerce(other) -> [Numeric] (18162.0)

自身と other が同じクラスになるよう、自身か other を変換し [other, self] という配列にして返します。

...her を Float に変換して [other, self] という配列にして返します。
Numeric
のサブクラスは、このメソッドを適切に再定義しなければなりません。
以下は Rational の coerce のソースです。other が自身の知らない数値クラスであった場...
...下さい。


//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

//}

数値クラスの算術演算子は通常自分と演算で...
...b/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 オペランドを数値で指定します。...