Module: UsefulUtilities::Size::Byte

Includes:
Standard::Binary
Included in:
UsefulUtilities::Size
Defined in:
lib/useful_utilities/size/byte.rb

Overview

Possible units:

:B  - bytes
:KB - kilobytes
:MB - megabytes
:GB - gigabytes
:TB - terabytes

Used ISO standard en.wikipedia.org/wiki/Binary_prefix

Binary
1 K = 1024

Constant Summary collapse

HALF_OF_SECTOR =
0.5

Constants included from Standard::Binary

Standard::Binary::GIBI, Standard::Binary::KIBI, Standard::Binary::MEBI, Standard::Binary::TEBI

Instance Method Summary collapse

Methods included from Standard::Binary

#to_binary_bi, #to_gibi, #to_kibi, #to_mebi, #to_tebi

Instance Method Details

#bytes_to_human_size(size, unit, rounder = 3) ⇒ Numeric

Returns humanized size in provided unit.

Parameters:

  • size (Numeric)
  • unit (Symbol)
  • rounder (Integer) (defaults to: 3)

Returns:

  • (Numeric)

    humanized size in provided unit



62
63
64
65
66
67
68
69
70
71
# File 'lib/useful_utilities/size/byte.rb', line 62

def bytes_to_human_size(size, unit, rounder = 3)
  case unit
  when :B  then size.round(rounder)
  when :KB then to_kilobytes(size, :B).round(rounder)
  when :MB then to_megabytes(size, :B).round(rounder)
  when :GB then to_gigabytes(size, :B).round(rounder)
  when :TB then to_terabytes(size, :B).round(rounder)
  else  unsupported_unit!(unit)
  end
end

#to_bytes(size, unit) ⇒ Numeric

Returns size in bytes.

Parameters:

Returns:



54
55
56
# File 'lib/useful_utilities/size/byte.rb', line 54

def to_bytes(size, unit)
  to_binary_bi(size, byte_prefix(unit))
end

#to_gigabytes(size, unit) ⇒ Numeric

Returns size in gigabytes.

Parameters:

Returns:



29
30
31
# File 'lib/useful_utilities/size/byte.rb', line 29

def to_gigabytes(size, unit)
  to_gibi(size, byte_prefix(unit))
end

#to_kilobytes(size, unit) ⇒ Numeric

Returns size in kilobytes.

Parameters:

Returns:



43
44
45
46
47
48
49
# File 'lib/useful_utilities/size/byte.rb', line 43

def to_kilobytes(size, unit)
  if unit == :sector
    return (size * HALF_OF_SECTOR).round # http://en.wikipedia.org/wiki/Disk_sector
  end

  to_kibi(size, byte_prefix(unit))
end

#to_megabytes(size, unit) ⇒ Numeric

Returns size in megabytes.

Parameters:

Returns:



36
37
38
# File 'lib/useful_utilities/size/byte.rb', line 36

def to_megabytes(size, unit)
  to_mebi(size, byte_prefix(unit))
end

#to_terabytes(size, unit) ⇒ Numeric

Returns size in terabytes.

Parameters:

Returns:



22
23
24
# File 'lib/useful_utilities/size/byte.rb', line 22

def to_terabytes(size, unit)
  to_tebi(size, byte_prefix(unit))
end