2020-03-07 01:30:57 +01:00
|
|
|
# frozen_string_literal: true
|
2013-05-03 15:32:08 +02:00
|
|
|
|
|
|
|
class Integer
|
|
|
|
def as_bytes
|
2020-03-02 01:57:58 +01:00
|
|
|
return '1 Byte' if self == 1
|
|
|
|
|
|
|
|
label = %w[Bytes KiB MiB GiB TiB]
|
2013-05-03 15:32:08 +02:00
|
|
|
i = 0
|
2020-03-02 01:57:58 +01:00
|
|
|
num = to_f
|
|
|
|
while num >= 1024
|
|
|
|
num /= 1024
|
2013-05-03 15:32:08 +02:00
|
|
|
i += 1
|
|
|
|
end
|
2020-03-02 01:57:58 +01:00
|
|
|
|
2013-05-03 15:32:08 +02:00
|
|
|
"#{format('%.2f', num)} #{label[i]}"
|
|
|
|
end
|
|
|
|
end
|