Module: Roebe::HumanReadable

Defined in:
lib/roebe/toplevel_methods/human_readable.rb

Class Method Summary collapse

Class Method Details

.[](i = 1000) ⇒ Object

#

Roebe::HumanReadable[]

The strange thing is, in bash, we seem to round up.

How many MB (megabyte) are in 1 GB (gigabyte)? 1024.

#


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/roebe/toplevel_methods/human_readable.rb', line 19

def self.[](i = 1000)
  i = i.to_f
  units = %w( B KB MB GB TB )
  e = 0
  unless i == 0
    e = ( Math.log(i) / Math.log(1024) ).floor
  end
  s = '%.3f' % (i.to_f / 1024.0 ** e) # Count up here. This will give us the proper values.
  s = s.to_f.ceil.to_s
  s = s.sub!(/\.?0*$/, '').to_s
  s = '0' if s.empty?
  _ = units[e] # Should be two chars, for better alignment.
  s << ' '+_.rjust(2) # Tap into units here.
  return s
end