Skip to content

Instantly share code, notes, and snippets.

@paolobrasolin
Created April 5, 2018 20:08
Show Gist options
  • Save paolobrasolin/eaffeda00df30cc490dc733509405d45 to your computer and use it in GitHub Desktop.
Save paolobrasolin/eaffeda00df30cc490dc733509405d45 to your computer and use it in GitHub Desktop.
def self.humanize_bytes(size)
%w[b Kb Mb Gb Tb Pb Eb Zb Yb].reduce(size) do |magnitude, unit|
break "#{magnitude}#{unit}" if magnitude < 1024
magnitude / 1024
end
end
def self.humanize_bytes(size)
return "#{size}b" if size < 1024
return "#{size}Kb" if (size /= 1024) < 1024
return "#{size}Mb" if (size /= 1024) < 1024
return "#{size}Gb" if (size /= 1024) < 1024
return "#{size}Tb" if (size /= 1024) < 1024
return "#{size}Pb" if (size /= 1024) < 1024
return "#{size}Eb" if (size /= 1024) < 1024
return "#{size}Zb" if (size /= 1024) < 1024
return "#{size}Yb" if size /= 1024
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment