るりまサーチ

最速Rubyリファレンスマニュアル検索!
33件ヒット [1-33件を表示] (0.152秒)

別のキーワード

  1. _builtin new
  2. _builtin inspect
  3. _builtin []
  4. _builtin to_s
  5. _builtin each

クラス

検索結果

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

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

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

デフォルトでは self と other を Float に変換して [other, self] という配列にして返します。
Numeric のサブクラスは、このメソ...
...al の coerce のソースです。other が自身の知らない数値クラスであった場合、
super を呼んでいることに注意して下さい。


//emlist[例][ruby]{
# lib/rational.rb より

def coerce(other)
if other.kind_of?(Float)
return other, self.to_f
e
lsif other.kind...
..._of?(Integer)
return Rational.new!(other, 1), self
e
lse
super
e
nd
e
nd
//}

数値クラスの算術演算子は通常自分と演算できないクラスをオペランドとして受け
取ると coerce を使って自分とオペランドを変換した上で演算を行います。...

Rational#coerce(other) -> Array (32321.0)

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

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

@
param other 比較または変換するオブジェクト

//emlist[例][ruby]{
Rational(1).coerce(2) # => [(2/1), (1/1)]
Rational(1).coerce(2.2) # => [2.2, 1....

Complex#coerce(other) -> [Complex, Complex] (32315.0)

other を Complex に変換して [変換後の other, self] の配列を返します。

...other を Complex に変換して [変換後の other, self] の配列を返します。

@
raise TypeError 変換できないオブジェクトを指定した場合に発生します。

//emlist[例][ruby]{
Complex(1).coerce(2) # => [(2+0i), (1+0i)]
//}...