Ruby 2.1.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > 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

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



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

インスタンスメソッド

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

積を計算します。

self ** rhs -> Numeric

@todo

mathn
self ** other -> Rational | Float

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

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

self を other で割った商を Float で返します。 other に虚数を指定することは出来ません。

floor(precision = 0) -> Integer | Rational

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

hash -> Integer

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

inspect -> String

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

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

小数点以下を切り捨てて値を整数に変換します。

to_r -> Rational

自身を返します。

to_s -> String

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

privateメソッド

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

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

marshal_dump -> Array

Marshal.#load のためのメソッドです。 Rational::compatible#marshal_load で復元可能な配列を返します。

継承したメソッド

! != __id__ __send__ instance_eval instance_exec method_missing singleton_method_added singleton_method_removed singleton_method_undefined % +@ -@ abs abs2 angle conj div divmod eql? i imag integer? nonzero? polar real real? rect remainder step to_c to_int zero? !~ === =~ _dump class clone define_singleton_method display enum_for equal? extend freeze frozen? initialize initialize_copy instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? itself marshal_load method 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? object_id pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods psych_to_yaml public_method public_methods public_send remove_instance_variable respond_to? respond_to_missing? send singleton_class singleton_method singleton_methods taint tainted? tap to_a to_ary to_hash to_io to_proc to_regexp to_str trust untaint untrust untrusted? .new .yaml_tag ::ARGF ::ARGV ::DATA ::ENV ::FALSE ::NIL ::RUBY_COPYRIGHT ::RUBY_DESCRIPTION ::RUBY_ENGINE ::RUBY_PATCHLEVEL ::RUBY_PLATFORM ::RUBY_RELEASE_DATE ::RUBY_REVISION ::RUBY_VERSION ::SCRIPT_LINES__ ::STDERR ::STDIN ::STDOUT ::TOPLEVEL_BINDING ::TRUE

追加されるメソッド

定義 説明 ライブラリ
json_create(hash) -> Rational

JSON のオブジェクトから Rational のオブジェクトを生成して返します。

json/add/rational
to_d(nFig) -> BigDecimal

自身を BigDecimal に変換します。

bigdecimal/util
to_json(*args) -> String

自身を JSON 形式の文字列に変換して返します。

json/add/rational