Skip to content

Instantly share code, notes, and snippets.

@ericdke
Created December 7, 2013 18:50
Show Gist options
  • Save ericdke/7846961 to your computer and use it in GitHub Desktop.
Save ericdke/7846961 to your computer and use it in GitHub Desktop.
bytes to kb,mb, etc
class Integer
def to_filesize
{
'B' => 1024,
'KB' => 1024 * 1024,
'MB' => 1024 * 1024 * 1024,
'GB' => 1024 * 1024 * 1024 * 1024,
'TB' => 1024 * 1024 * 1024 * 1024 * 1024
}.each_pair { |e, s| return "#{(self.to_f / (s / 1024)).round(2)}#{e}" if self < s }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment