Class: Rational

Inherits:
Object
  • Object
show all
Defined in:
lib/rational.rb

Overview

Monkey patch to add some formatting methods to Rational.

Author:

  • Julian Fiander

Since:

  • 0.1.5

Instance Method Summary collapse

Instance Method Details

#to_simplified_aArray

Converts Rational to Array

If Rational is an improper fraction, removes the integer part to convert to a mixed fraction.

Examples:

Mixed fraction

Rational(4,3).to_simplified_a #=> [1, Rational(1,3)]

Returns:

  • (Array)

    If less than 1, fraction. If greater than 1, a mixed fraction.

Since:

  • 0.1.5



29
30
31
32
33
34
35
36
# File 'lib/rational.rb', line 29

def to_simplified_a
  if self < 1
    to_s
  else
    truncated = self.truncate
    [truncated, (self - truncated)]
  end
end

#to_simplified_sString

Converts Rational to String

If Rational is an improper fraction, removes the integer part to convert to a mixed fraction.

Examples:

Mixed fraction

Rational(4,3).to_simplified_s #=> "1 1/3"

Returns:

  • (String)

    If less than 1, fraction. If greater than 1, a mixed fraction.

Since:

  • 0.1.5



13
14
15
16
17
18
19
20
# File 'lib/rational.rb', line 13

def to_simplified_s
  if self < 1
    to_s
  else
    truncated = self.truncate
    "#{truncated} #{self - truncated}"
  end
end