Module: BytesConverter
- Defined in:
- lib/bytes_converter.rb,
lib/bytes_converter/version.rb
Constant Summary collapse
- VERSION =
"0.0.6"
Class Method Summary collapse
-
.add_unit(new_unit) ⇒ Object
method adding new unit to @sizes hash.
-
.convert(size) ⇒ Object
method converting size into bytes.
-
.get_units ⇒ Object
getter for sizes.
-
.remove_unit(unit) ⇒ Object
method removing size.
Class Method Details
.add_unit(new_unit) ⇒ Object
method adding new unit to @sizes hash
43 44 45 46 |
# File 'lib/bytes_converter.rb', line 43 def self.add_unit new_unit @sizes.merge!(new_unit) true end |
.convert(size) ⇒ Object
method converting size into bytes
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bytes_converter.rb', line 15 def self.convert size # replacing comma with dot size_converted = size.downcase.gsub(',','.') # separating digits (size) from rest of the string (unit) x = size_converted.scan(/(\d*\.?\d*)(.*)/).collect { |found| found } # stripping whitespaces x[0][0].strip! x[0][1].strip! # limiting unit to first letter (k,m and so on) x[0][1] = x[0][1][/\w{1}/] # if unit is present (not nil) -> getting multiplier from sizes array # and calculating bytes unless x[0][1].nil? out = x[0][0].to_f * @sizes[x[0][1]].to_f else # if unit is not present -> we assume that size is already in bytes. out = x[0][0].to_f end # returning calculated (or not) value out end |
.get_units ⇒ Object
getter for sizes
49 50 51 |
# File 'lib/bytes_converter.rb', line 49 def self.get_units @sizes end |
.remove_unit(unit) ⇒ Object
method removing size
54 55 56 |
# File 'lib/bytes_converter.rb', line 54 def self.remove_unit unit @sizes.delete(unit) { |key| "Unit \"#{key}\" not found among sizes!" } end |