るりまサーチ

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

別のキーワード

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

ライブラリ

キーワード

検索結果

<< < 1 2 >>

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

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

...列にして返します。

デフォルトでは self と other を Float に変換して [other, self] という配列にして返します。
Numeric
のサブクラスは、このメソッドを適切に再定義しなければなりません。
以下は Rational の coerce のソースです...
...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

//}

数値クラスの算術演算子は通常自分と演算できないクラスをオペランドとして受け
取ると 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
else
x, y = a.coerce(self)
x + y
end

end

//}

@
param other オペラン...
<< < 1 2 >>