class Vagrant::Util::Numeric
Constants
- BYTES_CONVERSION_MAP
- EXABYTE
- GIGABYTE
- KILOBYTE
Authors Note: This conversion has been borrowed from the ActiveSupport
Numeric
class Conversion helper constants- LOGGER
- MEGABYTE
- PETABYTE
- SHORTHAND_MATCH_REGEX
Regex borrowed from the vagrant-disksize config class
- TERABYTE
Public Class Methods
bytes_to_megabytes(bytes)
click to toggle source
Rounds actual value to two decimal places
@param [Integer] bytes @return [Integer] megabytes - bytes representation in megabytes
# File lib/vagrant/util/numeric.rb, line 56 def bytes_to_megabytes(bytes) (bytes / MEGABYTE.to_f).round(2) end
reset!()
click to toggle source
@private Reset the cached values for platform. This is not considered a public API and should only be used for testing.
# File lib/vagrant/util/numeric.rb, line 63 def reset! instance_variables.each(&method(:remove_instance_variable)) end
string_to_bytes(str)
click to toggle source
A helper that converts a shortcut string to its bytes representation. The expected format of `str` is essentially: “<Number>XX” Where `XX` is shorthand for KB, MB, GB, TB, PB, or EB. For example, 50 megabytes:
str = “50MB”
@param [String] - str @return [Integer,nil] - bytes - returns nil if method fails to convert to bytes
# File lib/vagrant/util/numeric.rb, line 33 def string_to_bytes(str) bytes = nil str = str.to_s.strip matches = SHORTHAND_MATCH_REGEX.match(str) if matches number = matches[:number].to_i unit = matches[:unit].to_sym if BYTES_CONVERSION_MAP.key?(unit) bytes = number * BYTES_CONVERSION_MAP[unit] else LOGGER.error("An invalid unit or format was given, string_to_bytes cannot convert #{str}") end end bytes end