Puppet Function: format_bytes
- Defined in:
- lib/puppet/functions/format_bytes.rb
- Function type:
- Ruby 4.x API
Summary
Converts the bytes argument into a human-readable form, for example 1000000000 bytes becomes 1GB.Overview
format_bytes.rb
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/puppet/functions/format_bytes.rb', line 4 Puppet::Functions.create_function(:format_bytes) do # @summary Converts the bytes argument into a human-readable form, for example 1000000000 bytes becomes 1GB. # @param bytes The size in bytes # @return A string containing a human-readable representation of bytes using the most appropriate unit dispatch :format_bytes do param 'Integer', :bytes end def format_bytes(bytes) powers = { 3 => 'k', 6 => 'M', 9 => 'G', 12 => 'T', 15 => 'P', 18 => 'E', } bytes = bytes.to_i power = 0 while bytes > 1000 bytes /= 1000.0 power += 3 end "#{'%.2f' % bytes} #{powers[power]}B" end end |