module PalmCivet

Constants

BYTE
BYTESPATTERN
GIGABYTE
KILOBYTE
MEGABYTE
TERABYTE
VERSION

Public Class Methods

byte_size(bytes) click to toggle source

Returns a human-readable byte string of the form 10M, 12.5K, and so forth. The following units are available:

  • T: Terabyte

  • G: Gigabyte

  • M: Megabyte

  • K: Kilobyte

  • B: Byte

The unit that results in the smallest number greater than or equal to 1 is always chosen.

# File lib/palm_civet.rb, line 26
def self.byte_size(bytes)
  if !bytes.is_a? Numeric
    raise TypeError, "must be an integer or float"
  end

  case
  when bytes >= TERABYTE
    unit = "T"
    value = bytes / TERABYTE
  when bytes >= GIGABYTE
    unit = "G"
    value = bytes / GIGABYTE
  when bytes >= MEGABYTE
    unit = "M"
    value = bytes / MEGABYTE
  when bytes >= KILOBYTE
    unit = "K"
    value = bytes / KILOBYTE
  when bytes >= BYTE
    unit = "B"
    value = bytes
  else
    return "0"
  end

  value = "%g" % ("%.1f" % value)
  return value << unit
end
to_bytes(bytes) click to toggle source

Parses a string formatted by bytes_size as bytes. Note binary-prefixed and SI prefixed units both mean a base-2 units:

  • KB = K = KiB = 1024

  • MB = M = MiB = 1024 * K

  • GB = G = GiB = 1024 * M

  • TB = T = TiB = 1024 * G

# File lib/palm_civet.rb, line 61
def self.to_bytes(bytes)
  matches = BYTESPATTERN.match(bytes.strip)
  if matches == nil
    raise InvalidByteQuantityError
  end

  value = Float(matches[1])

  case matches[2][0].capitalize
  when "T"
    value = value * TERABYTE
  when "G"
    value = value * GIGABYTE
  when "M"
    value = value * MEGABYTE
  when "K"
    value = value * KILOBYTE
  end

  return value.to_i
rescue TypeError
  raise InvalidByteQuantityError
end
to_megabytes(bytes) click to toggle source

Parses a string formatted by byte_size as megabytes.

# File lib/palm_civet.rb, line 86
def self.to_megabytes(bytes)
  (self.to_bytes(bytes) / MEGABYTE).to_i
end