Ruby 1.9.2 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Rationalクラス

class Rational

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

要約

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

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

例:

Rational(1, 3)       # => (1/3)
Rational('1/3')      # => (1/3)
Rational('0.33')     # => (33/100)
Rational.new(1, 3)   # => NoMethodError

ただし、1.8系とは異なり、Rational オブジェクトは常に既約(それ以上 約分できない状態)である事に注意してください。

Rational(2, 6)       # => (1/3)
Rational(1, 3) * 3   # => (1/1)

インスタンスメソッド

定義 説明
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
quo(other) -> Rational | Float

商を計算します。

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

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

self == other -> bool

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

ceil(precision = 0) -> Integer | Rational

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

coerce(other) -> Array

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

denominator -> Integer

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

fdiv(other) -> Float

自身を other で割った実数の商を返します。

floor(precision = 0) -> Integer | Rational

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

hash -> Integer

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

inspect -> String

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

marshal_dump -> Array

Rational#marshal_load で復元可能な配列を返します。

marshal_load -> Rational

Rational#marshal_dump で得られた配列を基に、Rational オブジェ クトを復元します。

numerator -> Integer

分子を返します。

rationalize(eps = 0) -> Rational

自身から eps で指定した許容誤差の範囲に収まるような Rational を返 します。

round(precision = 0) -> Integer | Rational

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

to_f -> Float

自身を Float に変換します。

to_i -> Integer
truncate(precision = 0) -> Rational | Integer

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

to_r -> Rational

自身を返します。

to_s -> String

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

privateメソッド

定義 説明
convert(*arg) -> Rational

引数を有理数(Rational)に変換した結果を返します。

継承したメソッド

! != +@ -@ abs clone div divmod eql? i 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? method method_missing methods must_be must_be_close_to must_be_empty must_be_instance_of must_be_kind_of must_be_nil must_be_same_as must_be_within_epsilon must_equal must_include must_match must_raise must_respond_to must_send must_throw 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? respond_to_missing? singleton_class 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 trust untaint untrust untrusted? wont_be wont_be_close_to wont_be_empty wont_be_instance_of wont_be_kind_of wont_be_nil wont_be_same_as wont_be_within_epsilon wont_equal wont_include wont_match wont_respond_to .new

追加されるメソッド

定義 説明 ライブラリ
to_d(nFig = 0) -> BigDecimal

自身を BigDecimal に変換します。

bigdecimal/util