Sha256: 8825e1b24a7739d92c6477a13695f85081314b786cffeec318bb3c58b940e96a

Contents?: true

Size: 858 Bytes

Versions: 3

Compression:

Stored size: 858 Bytes

Contents

module Reality
  # @private
  module Util
    module Format
      module_function

      def number(n)
        case n.abs
        when 0..1
          n.to_f.to_s.sub(/(\.0*[1-9]).*$/, '\1')
        when 1..100
          ('%.2f' % n).sub(/\.?0+$/, '')
        when 1_000..Float::INFINITY
          # see http://stackoverflow.com/a/6460145/3683228
          n.to_i.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
        else
          n.to_i.to_s
        end
      end

      def describe(title, hash)
        # hash may be an array, in fact :)
        key_width = hash.map(&:first).map(&:length).max.to_i + 1

        [
          '-' * title.length,
          title,
          '-' * title.length,
          *hash.sort_by(&:first).map{|key, value|
            "#{key.to_s.rjust(key_width)}: #{value}"
          }
        ].join("\n")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reality-0.1.0.alpha3 lib/reality/util/formatters.rb
reality-0.1.0.alpha2 lib/reality/util/formatters.rb
reality-0.1.0.alpha lib/reality/util/formatters.rb