I wasn't able to find this function in Rails. Probably it
already exists somewhere- if not, maybe this will save
someone 2 minutes
It's in ActionView::Helpers::NumberHelper.
It's even called human_size via an alias. If you had tried it in a view
before coding it, it would have worked! That has happened to me a couple
times. Rails rocks.
FYI: Here's the implementation. Doesn't go past TB.
# File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb,
line 87
def number_to_human_size(size)
case
when size < 1.kilobyte: '%d Bytes' % size
when size < 1.megabyte: '%.1f KB' % (size / 1.0.kilobyte)
when size < 1.gigabyte: '%.1f MB' % (size / 1.0.megabyte)
when size < 1.terabyte: '%.1f GB' % (size / 1.0.gigabyte)
else '%.1f TB' % (size / 1.0.terabyte)
end.sub('.0', '')
rescue
nil
end