Ruby 1.8.7 リファレンスマニュアル > ライブラリ一覧 > rationalライブラリ > Rationalクラス

class Rational

クラスの継承リスト: Rational < Numeric < Comparable < Object < Kernel

要約

有理数を扱うクラスです。

「1/3」のような有理数を扱う事ができます。IntegerFloat と同様に Rational.new ではなく、 Kernel.#Rational を使用して Rational オブジェクトを作成します。

Integer < Rational < Float の順に強いです。つまり other が Float なら、 self を Float に変換してから演算子を適用します。other が Integer なら other を Rational に変換してから演算子を適用します。冪乗は例外です。

特異メソッド

定義 説明
new!(num, den = 1) -> Rational

Rational オブジェクトを生成します。

reduce(num, den = 1) -> Rational

約分された Rational オブジェクトを生成します。

インスタンスメソッド

定義 説明
self % other -> Rational | Float

剰余を計算します。絶対値が self の絶対値を越えない、符号が self と同じ Numeric を返します。

self * other -> Rational | Float

積を計算します。

self ** other -> Rational | Float

冪(べき)乗を計算します。

self ** rhs -> Numeric

self のべき乗を返します。 Rational になるようであれば Rational で返します。

mathn
self + other -> Rational | Float

和を計算します。

self - other -> Rational | Float

差を計算します。

self / other -> Rational | Float

商を計算します。

self <=> other -> -1 | 0 | 1 | nil

self と other を比較して、self が大きい時に 1、等しい時に 0、小さい時に -1 を返します。比較できない場合はnilを返します。

self == other -> bool

数値として等しいか判定します。

abs -> Rational

自身の絶対値を返します。

ceil -> Integer

自身と等しいかより大きな整数のうち最小のものを返します。

coerce(other) -> Array

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

denominator -> Integer

分母を返します。常に正の整数を返します。

div(other) -> Integer

self を other で割った整数の商を返します。

divmod(other) -> [Integer, Float | Rational]

self を other で割った、商と余りの配列を返します。

floor -> Integer

自身と等しいかより小さな整数のうち最大のものを返します。

hash -> Integer

自身のハッシュ値を返します。

inspect

有理数値を人間が読みやすい形の文字列表現にして返します。

mathn
inspect -> String

自身を"Rational(分子, 分母)" 形式の文字列にして返します。

numerator -> Integer

分子を返します。

power2
作りかけ(^^;;
mathn
round -> Integer

自身ともっとも近い整数を返します。

to_f -> Float

自身を Float に変換します。

truncate -> Integer
to_i -> Integer

0 から 自身までの整数で、自身にもっとも近い整数を返します。

to_r -> Rational

自身を返します。

to_s -> String

自身を人間が読みやすい形の文字列表現にして返します。

継承したメソッド

+@ -@ arg clone conj eql? fdiv im imag integer? modulo nonzero? real remainder step to_int zero? === =~ __id__ __send__ _dump class dclone display enum_for equal? extend freeze frozen? initialize initialize_copy instance_eval instance_exec instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? marshal_dump marshal_load method method_missing methods nil? pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods public_methods remove_instance_variable respond_to? singleton_method_added singleton_method_removed singleton_method_undefined singleton_methods taint tainted? tap to_a to_ary to_hash to_io to_proc to_regexp to_str to_yaml to_yaml_properties to_yaml_style untaint .new

追加されるメソッド

定義 説明 ライブラリ
Unify

内部実装で利用しています。深くは考えないでください。ユーザープログラムでは利用しないでください。

mathn
to_d(nFig = 0) -> BigDecimal

自身を BigDecimal に変換します。

bigdecimal/util