class StaticdUtils::FileSize

Class to convert file size in octect to human readable size.

Example:

Staticd::FileSize.new(1000).to_s
# => "1KB"

Public Class Methods

new(size) click to toggle source
# File lib/staticd_utils/file_size.rb, line 10
def initialize(size)
  @size = size
end

Public Instance Methods

to_s() click to toggle source
# File lib/staticd_utils/file_size.rb, line 14
def to_s
  units = %w(B KB MB GB TB)
  base = 1000
  return "#{@size}#{units[0]}" if @size < base

  exponent = (Math.log(@size) / Math.log(base)).to_i
  exponent = units.size - 1 if exponent > units.size - 1

  human_size = @size / base**exponent
  "#{human_size}#{units[exponent]}"
end