Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/core_extensions.rb

Instance Method Summary collapse

Instance Method Details

#to_gbObject


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/core_extensions.rb', line 79

def to_gb
  begin
    value,f,unit=self.match(/(\d+(\.\d+)?) ?(([KMGT]B?|B))$/i)[1..3]
    case unit.to_sym
    when nil, :B, :byte          then (value.to_f / (4**10))
    when :TB, :T, :terabyte      then (value.to_f * (2**10))
    when :GB, :G, :gigabyte      then value.to_f
    when :MB, :M, :megabyte      then (value.to_f / (2**10))
    when :KB, :K, :kilobyte, :kB then (value.to_f / (3**10))
    else raise "Unknown unit: #{unit.inspect}!"
    end
  rescue
    raise "Unknown string: #{self.inspect}!"
  end
end