Class Rational
In: lib/mathn.rb
lib/rational.rb
Parent: Object

Rational implements a rational class for numbers.

A rational number is a number that can be expressed as a fraction p/q where p and q are integers and q != 0. A rational number p/q is said to have numerator p and denominator q. Numbers that are not rational are called irrational numbers. (mathworld.wolfram.com/RationalNumber.html)

To create a Rational Number:

  Rational(a,b)             # -> a/b
  Rational.new!(a,b)        # -> a/b

Examples:

  Rational(5,6)             # -> 5/6
  Rational(5)               # -> 5/1

Rational numbers are reduced to their lowest terms:

  Rational(6,10)            # -> 3/5

But not if you use the unusual method "new!":

  Rational.new!(6,10)       # -> 6/10

Division by zero is obviously not allowed:

  Rational(3,0)             # -> ZeroDivisionError

Methods

%   *   **   **   +   -   /   <=>   ==   abs   coerce   divmod   hash   inspect   inspect   new   new!   power2   reduce   to_f   to_i   to_r   to_s  

Constants

Unify = true

External Aliases

** -> power!

Attributes

denominator  [R] 
numerator  [R] 

Public Class methods

This method is actually private.

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.

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

Public Instance methods

Returns the remainder when this value is divided by other.

Examples:

  r = Rational(7,4)    # -> Rational(7,4)
  r % Rational(1,2)    # -> Rational(1,4)
  r % 1                # -> Rational(3,4)
  r % Rational(1,7)    # -> Rational(1,28)
  r % 0.26             # -> 0.19

Returns the product of this value and a.

Examples:

  r = Rational(3,4)    # -> Rational(3,4)
  r * 2                # -> Rational(3,2)
  r * 4                # -> Rational(3,1)
  r * 0.5              # -> 0.375
  r * Rational(1,2)    # -> Rational(3,8)

Returns this value raised to the given power.

Examples:

  r = Rational(3,4)    # -> Rational(3,4)
  r ** 2               # -> Rational(9,16)
  r ** 2.0             # -> 0.5625
  r ** Rational(1,2)   # -> 0.866025403784439

Returns the addition of this value and a.

Examples:

  r = Rational(3,4)      # -> Rational(3,4)
  r + 1                  # -> Rational(7,4)
  r + 0.5                # -> 1.25

Returns the difference of this value and a. subtracted.

Examples:

  r = Rational(3,4)    # -> Rational(3,4)
  r - 1                # -> Rational(-1,4)
  r - 0.5              # -> 0.25

Returns the quotient of this value and a.

  r = Rational(3,4)    # -> Rational(3,4)
  r / 2                # -> Rational(3,8)
  r / 2.0              # -> 0.375
  r / Rational(1,2)    # -> Rational(3,2)

Standard comparison operator.

Returns true iff this value is numerically equal to other.

But beware:

  Rational(1,2) == Rational(4,8)          # -> true
  Rational(1,2) == Rational.new!(4,8)     # -> false

Don‘t use Rational.new!

Returns the absolute value.

Returns the quotient and remainder.

Examples:

  r = Rational(7,4)        # -> Rational(7,4)
  r.divmod Rational(1,2)   # -> [3, Rational(1,4)]

Returns a hash code for the object.

Returns a reconstructable string representation:

  Rational(5,8).inspect     # -> "Rational(5, 8)"

Converts the rational to a Float.

Converts the rational to an Integer. Not the nearest integer, the truncated integer. Study the following example carefully:

  Rational(+7,4).to_i             # -> 1
  Rational(-7,4).to_i             # -> -2
  (-1.75).to_i                    # -> -1

In other words:

  Rational(-7,4) == -1.75                 # -> true
  Rational(-7,4).to_i == (-1.75).to_i     # false

Returns self.

Returns a string representation of the rational number.

Example:

  Rational(3,4).to_s          #  "3/4"
  Rational(8).to_s            #  "8"

[Validate]

ruby-doc.org is a service of James Britt and Neurogami, a Ruby application development company in Phoenix, AZ.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.