Sha256: 30d4842d320c5cae91eb953d2041ecdffafca06e30fe6756915da87c67447224

Contents?: true

Size: 1014 Bytes

Versions: 44

Compression:

Stored size: 1014 Bytes

Contents

# Monkey patch to add some formatting methods to Rational.
# 
# @author Julian Fiander
# @since 0.1.5
class Rational
  # Converts Rational to String
  #
  # If Rational is an improper fraction, removes the integer part to convert to a mixed fraction.
  #
  # @example Mixed fraction
  #   Rational(4,3).to_simplified_s #=> "1 1/3"
  # @return [String] If less than 1, fraction. If greater than 1, a mixed fraction.
  def to_simplified_s
    if self < 1
      to_s
    else
      truncated = self.truncate
      "#{truncated} #{self - truncated}"
    end
  end

  # Converts Rational to Array
  #
  # If Rational is an improper fraction, removes the integer part to convert to a mixed fraction.
  #
  # @example Mixed fraction
  #   Rational(4,3).to_simplified_a #=> [1, Rational(1,3)]
  # @return [Array] If less than 1, fraction. If greater than 1, a mixed fraction.
  def to_simplified_a
    if self < 1
      to_s
    else
      truncated = self.truncate
      [truncated, (self - truncated)]
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
usps_flags-0.3.26 lib/rational.rb
usps_flags-0.3.25 lib/rational.rb
usps_flags-0.3.24 lib/rational.rb
usps_flags-0.3.23 lib/rational.rb
usps_flags-0.3.22 lib/rational.rb
usps_flags-0.3.21 lib/rational.rb
usps_flags-0.3.20 lib/rational.rb
usps_flags-0.3.19 lib/rational.rb
usps_flags-0.3.18 lib/rational.rb
usps_flags-0.3.17 lib/rational.rb
usps_flags-0.3.16 lib/rational.rb
usps_flags-0.3.15 lib/rational.rb
usps_flags-0.3.14 lib/rational.rb
usps_flags-0.3.13 lib/rational.rb
usps_flags-0.3.12 lib/rational.rb
usps_flags-0.3.11 lib/rational.rb
usps_flags-0.3.10 lib/rational.rb
usps_flags-0.3.9 lib/rational.rb
usps_flags-0.3.8 lib/rational.rb
usps_flags-0.3.7 lib/rational.rb