Created
April 5, 2018 20:08
-
-
Save paolobrasolin/eaffeda00df30cc490dc733509405d45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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