Sha256: 0ea82471ad9fbf2b4135ab6efc2ace27a2491b800ada9cfbfb6b841545b21de0

Contents?: true

Size: 1.12 KB

Versions: 14

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: false

# Monkey patch to add some formatting methods to Rational.
#
# @author Julian Fiander
# @since 0.1.5
class USPSFlags
  class Rational
    def initialize(rational)
      @rational = rational
    end

    # 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
      @rational < 1 ? @rational.to_s : [@rational.truncate.to_s, (@rational - @rational.truncate).to_s].join(' ')
    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
      @rational < 1 ? @rational.to_s : [@rational.truncate, (@rational - @rational.truncate)]
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
usps_flags-0.6.4 lib/usps_flags/rational.rb
usps_flags-0.6.3 lib/usps_flags/rational.rb
usps_flags-0.6.2 lib/usps_flags/rational.rb
usps_flags-0.6.1 lib/usps_flags/rational.rb
usps_flags-0.6.0 lib/usps_flags/rational.rb
usps_flags-0.5.9 lib/usps_flags/rational.rb
usps_flags-0.5.8 lib/usps_flags/rational.rb
usps_flags-0.5.7 lib/usps_flags/rational.rb
usps_flags-0.5.6 lib/usps_flags/rational.rb
usps_flags-0.5.5 lib/usps_flags/rational.rb
usps_flags-0.5.4 lib/usps_flags/rational.rb
usps_flags-0.5.3 lib/usps_flags/rational.rb
usps_flags-0.5.2 lib/usps_flags/rational.rb
usps_flags-0.5.1 lib/usps_flags/rational.rb