Rails file size pretty printing

назад до списку постів

I usually write posts in Ukrainian but this one should be understood also by people of other nationalities.

I was wandering when i have not found rails view helper for pretty displaying of file sizes (e.g. bytes, kilobytes etc.) and decided to make my own. To see what i mean watch next code snippet from the README of plugin

pretty_file_size(1023) => '1023 b'
pretty_file_size(1024) => '1 Kb'

pretty_file_size(123.123) => '123.12 b'
pretty_file_size(123.123 :accuracy => 3) => '123.123 b'

pretty_file_size(1024) do |size, unit|
  # size = 1 unit = " Mb"
  "Size is " + size + " and the unit is" + unit
end => "Size is 1 and the unit is Mb"

pretty_file_size(1024, :names => { :kb => " Kilobyte" }) => '1 Kilobyte'

As you can see pretty_file_size takes size in bytes and translates it to corresponding string. Also it has additional options.

And here is the url of repository with plugin http://github.com/html/pretty_file_size