Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/human_size_to_number/helper.rb
Defined Under Namespace
Classes: InvalidSizeStringError
Instance Method Summary collapse
Instance Method Details
#human_size_to_number(options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/human_size_to_number/helper.rb', line 12 def human_size_to_number(={}) size, unit = self.scan(/(\d*\.?\d+)\s?(Bytes?|KB|MB|GB|TB)/).first number = begin Float(size) rescue ArgumentError, TypeError if [:raise] raise InvalidSizeStringError, size else return size end end number = case unit when "Byte", "Bytes" number when "KB" number * 1024 when "MB" number * 1024 * 1024 when "GB" number * 1024 * 1024 * 1024 when "TB" number * 1024 * 1024 * 1024 * 1024 end number.round end |