るりまサーチ

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

別のキーワード

  1. _builtin rational
  2. rational **
  3. json/add/rational to_json
  4. json/add/rational json_create

ライブラリ

クラス

検索結果

Rational#coerce(other) -> Array (27126.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.0]
//}...

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

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

...ければなりません。
以下は Rational coerce のソースです。other が自身の知らない数値クラスであった場合、
super を呼んでいることに注意して下さい。


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

def coerce(other)
if other.kind_of?(Float)...
...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)
# 長い...

BigDecimal#coerce(other) -> Array (18120.0)

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

...ther 比較または変換するオブジェクト

BigDecimal#coerce は Ruby における強制型変換のための機能です。
BigDecimal オブジェクトとその他のオブジェクト間の各種の計算は
BigDecimal#coerce の結果を元に行われます。

//emlist[][ruby]{
requi...
...re "bigdecimal"
a = BigDecimal("1.0")
b = a / 2.0 # => 0.5e0
//}

other に Rational オブジェクトを指定した場合は self の有効桁数を
用いて変換を行います。

数値を表す文字列から BigDecimal オブジェクトに変換する機能はデフォ
ルトでは無効...

Matrix#coerce(other) -> Array (18114.0)

他の数値オブジェクトとの変換を行います。

...との組を配列として返します。

@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:0x832df18 @value=(1/2)>, Matrix[[1, 2], [-1.25, 2.2]]]
//}...