Skip to content

Instantly share code, notes, and snippets.

@epitron
Created November 10, 2012 08:33
Show Gist options
  • Save epitron/4050444 to your computer and use it in GitHub Desktop.
Save epitron/4050444 to your computer and use it in GitHub Desktop.
Bytes to human-readable format.
class Numeric
SIZE_TABLE = {
# power # units
0 => "",
1 => "KB",
2 => "MB",
3 => "GB",
4 => "TB",
5 => "PB",
6 => "EB",
7 => "ZB",
8 => "YB",
}
def human_size
power = self.log(1024).floor
base = 1024 ** power
units = SIZE_TABLE[power]
"#{(self / base).round}#{units}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment