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

class Rational

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

要約

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

特異メソッド

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

Implements the constructor. This method does not reduce to lowest terms or check for division by zero. Therefore #Rational() should be preferred in normal use.

reduce(num, den = 1)

Reduces the given numerator and denominator to their lowest terms. Use Rational() instead.

インスタンスメソッド

定義 説明
self % other

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

self * other

積を計算します。

self ** other

冪を計算します。

self **

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

mathn
self + other

和を計算します。

self - other

差を計算します。

self / other

商を計算します。 other が 0 の時は、例外 ZeroDivisionError を投げます。

self <=> other

other と比べて self が大きいなら 1、同じなら 0、小さいなら -1 を返します。

abs

self が正なら self、負なら -1 * self を返します。

denominator

分母を Fixnum として返します。

divmod(other)

self を other で割った、商と余りの配列を返します。 商は Fixnum、余りは絶対値が other の絶対値を越えず、符号が other と同じ Numeric です。Numeric#divmod も参照して下さい。

inspect

「3/5」などの形で返します。

mathn
numerator

分子を Fixnum として返します。

power2
作りかけ(^^;;
mathn
to_f

Float に変換します。

to_i

Fixnum に変換します。

to_s

文字列に変換します。

継承したメソッド

! != +@ -@ arg ceil clone coerce conj div eql? fdiv floor im imag integer? modulo nonzero? real remainder round scalar? step to_int truncate zero? == === =~ __id__ __send__ _dump class dclone display enum_for equal? extend freeze frozen? hash 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_splat to_str to_yaml to_yaml_properties to_yaml_style untaint .new

追加されるメソッド

定義 説明 ライブラリ
Unify mathn
to_d(nFig = 0)

BigDecimalに変換する

bigdecimal/util