module UsefulUtilities::Size::Byte
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
Constants
- HALF_OF_SECTOR
Public Instance Methods
bytes_to_human_size(size, unit, rounder = 3)
click to toggle source
@param size [Numeric] @param unit [Symbol] @param rounder [Integer] @return [Numeric] humanized size in provided unit
# 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)
click to toggle source
@param size [Numeric] @param unit [Symbol] @return [Numeric] size in bytes
# 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)
click to toggle source
@param size [Numeric] @param unit [Symbol] @return [Numeric] size in gigabytes
# 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)
click to toggle source
@param size [Numeric] @param unit [Symbol] @return [Numeric] size in kilobytes
# 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)
click to toggle source
@param size [Numeric] @param unit [Symbol] @return [Numeric] size in megabytes
# 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)
click to toggle source
@param size [Numeric] @param unit [Symbol] @return [Numeric] size in terabytes
# File lib/useful_utilities/size/byte.rb, line 22 def to_terabytes(size, unit) to_tebi(size, byte_prefix(unit)) end
Private Instance Methods
byte_prefix(unit)
click to toggle source
# File lib/useful_utilities/size/byte.rb, line 75 def byte_prefix(unit) case unit when :B then :B when :KB then :KiB when :MB then :MiB when :GB then :GiB when :TB then :TiB else unsupported_unit!(unit) end end
unsupported_unit!(unit)
click to toggle source
# File lib/useful_utilities/size/byte.rb, line 86 def unsupported_unit!(unit) raise ArgumentError.new("Unsupported unit - #{ unit }") end