Sha256: 28f4af5dfcc9474943d1c29a2b0cc11e04a2ce3d7fe3548026e2126773279e9a

Contents?: true

Size: 1.05 KB

Versions: 12

Compression:

Stored size: 1.05 KB

Contents

require_relative 'constants/ru'

module Humanize
  class Ru
    def humanize(number)
      iteration = 0
      parts = []
      use_and = false
      until number.zero?
        number, remainder = number.divmod(1000)
        unless remainder.zero?
          if iteration.zero? && remainder < 100
            use_and = true
          else
            add_grouping(parts, use_and, iteration)
          end

          parts << SUB_ONE_GROUPING[remainder]
        end

        iteration += 1
      end

      correct_lots(parts)
    end

    private

    def conjunction(parts, use_and)
      return '' if parts.empty? || !use_and

      ', '
    end

    def add_grouping(parts, use_and, iteration)
      grouping = LOTS[iteration]
      return unless grouping

      parts << "#{grouping}#{conjunction(parts, use_and)}"
    end

    def correct_lots(parts)
      parts = parts.reverse.join(' ').squeeze(' ')
      SPECIFIC_LOTS.each do |wrong, right|
        parts = parts.sub(wrong, right) if parts.include?(wrong)
      end

      parts.split(' ').reverse
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
humanize-2.5.1 lib/humanize/locales/ru.rb
humanize-2.5.0 lib/humanize/locales/ru.rb
humanize-2.4.3 lib/humanize/locales/ru.rb
humanize-2.4.2 lib/humanize/locales/ru.rb
humanize-2.4.1 lib/humanize/locales/ru.rb
humanize-2.4.0 lib/humanize/locales/ru.rb
humanize-2.3.0 lib/humanize/locales/ru.rb
humanize-2.2.1 lib/humanize/locales/ru.rb
humanize-2.2.0 lib/humanize/locales/ru.rb
humanize-2.1.2 lib/humanize/locales/ru.rb
humanize-2.1.1 lib/humanize/locales/ru.rb
humanize-2.1.0 lib/humanize/locales/ru.rb