Sha256: 7e1373a9137b51979665d3e78ab0b0108f4a1378857b50f42580a4bb310ca502

Contents?: true

Size: 972 Bytes

Versions: 3

Compression:

Stored size: 972 Bytes

Contents

require 'facet/bytes'
#require 'facet/numeric/bytes'

class Numeric
  # Provides formated output of bytes proportial size.
  #
  #   require 'facet/numeric/octet_units'
  #
  #   1024.octet_units            #=> "1.00 KB"
  #   1048576.octet_units         #=> "1.00 MB"
  #   1073741824.octet_units      #=> "1.00 GB"
  #   1099511627776.octet_units   #=> "1.00 TB"
  #
  # Takes a format string to adjust output.
  #
  #   1024.octet_units('%.0f')    #=> "1 KB"
  #
  # Note: the name of this method will likely change in the future.
  # But the best name has yet to be determined at this point.
  def octet_units(fmt='%.2f')
    case
    when self < 1.kilobyte
      "#{self} bytes"
    when self < 1.megabyte
      "#{fmt % (self.to_f / 1.kilobyte)} KB"
    when self < 1.gigabyte
      "#{fmt % (self.to_f / 1.megabyte)} MB"
    when self < 1.terabyte
      "#{fmt % (self.to_f / 1.gigabyte)} GB"
    else
      "#{fmt % (self.to_f / 1.terabyte)} TB"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-0.7.0 lib/facet/numeric/octet_units.rb
facets-0.7.1 lib/facet/numeric/octet_units.rb
facets-0.7.2 lib/facet/numeric/octet_units.rb