Class

Rational

Inheritance
< 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

Constants

Name   Description
Unify = true

Attributes

Name Visibility R/W Description
denominator public R
numerator public R

Aliases

Method Alias Description
** → power!

Methods

Class

Visibility Signature
public new (num, den)
public new! (num, den = 1)
public reduce (num, den = 1)

Instance

Visibility Signature
public % (other)
public * (a)
public ** (other)
public ** (other)
public + (a)
public - (a)
public / (a)
public <=> (other)
public == (other)
public abs ()
public ceil ()
public coerce (other)
public div (other)
public divmod (other)
public floor ()
public hash ()
public inspect ()
public inspect ()
public power2 (other)
public round ()
public to_f ()
public to_i ()
public to_r ()
public to_s ()
public truncate ()

Class Method Detail

new(num, den)

This method is actually private.

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.

Instance Method Detail

%(other)

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

*(a)

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)

**(other)

**(other)

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

+(a)

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

-(a)

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

/(a)

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)

<=>(other)

Standard comparison operator.

==(other)

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!

abs()

Returns the absolute value.

ceil()

coerce(other)

div(other)

divmod(other)

Returns the quotient and remainder.

Examples:

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

floor()

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             # -> -1
  (-1.75).to_i                    # -> -1

In other words:

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

hash()

Returns a hash code for the object.

inspect()

inspect()

Returns a reconstructable string representation:

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

power2(other)

round()

to_f()

Converts the rational to a Float.

to_i()

Alias for truncate

to_r()

Returns self.

to_s()

Returns a string representation of the rational number.

Example:

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

truncate()