Module: ByteSize

Included in:
Bignum, Fixnum, Float
Defined in:
lib/rcs-common/fixnum.rb

Constant Summary collapse

KiB =
2**10
MiB =
2**20
GiB =
2**30
TiB =
2**40
KB =
10**3
MB =
10**6
GB =
10**9
TB =
10**12

Instance Method Summary collapse

Instance Method Details

#to_s_bytes(base = 2) ⇒ Object

return the size in a human readable format



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rcs-common/fixnum.rb', line 15

def to_s_bytes(base = 2)

  base_two = {TiB => 'TiB', GiB => 'GiB', MiB => 'MiB', KiB => 'KiB'}
  base_ten = {TB => 'TB', GB => 'GB', MB => 'MB', KB => 'kB'}

  values = base_two if base == 2
  values = base_ten if base == 10

  values.each_pair do |k, v|
    if self >= k
      return (self.to_f / k).round(2).to_s + ' ' + v
    end
  end

  # case when is under KiB
  return self.to_s + ' B'

end