Hi I wasn''t able to find this function in Rails. Probably it already exists somewhere- if not, maybe this will save someone 2 minutes def human_size(size) if size < 1.kilobyte size + " Bytes" elsif size < 1.megabyte sprintf("%.0f KB", size / 1.kilobyte.to_f) elsif size < 1.gigabyte sprintf("%.1f MB", size / 1.megabyte.to_f) elsif size < 1.terabyte sprintf("%.1f GB", size / 1.gigabyte.to_f) elsif size < 1.petabyte sprintf("%.1f TB", size / 1.terabyte.to_f) elsif size < 1.exabyte sprintf("%.1f PB", size / 1.petabyte.to_f) else sprintf("%.1f EB", size / 1.exabyte.to_f) end end Ryan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I wasn''t able to find this function in Rails. Probably it > already exists somewhere- if not, maybe this will save > someone 2 minutesIt''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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Hi > I wasn''t able to find this function in Rails. Probably it already > exists somewhere- if not, maybe this will save someone 2 minutesIt is... number_to_human_size(size) Returns a formatted-for-humans file size. Examples: human_size(123) => 123 Bytes human_size(1234) => 1.2 KB human_size(12345) => 12.1 KB human_size(1234567) => 1.2 MB human_size(1234567890) => 1.1 GB> > def human_size(size) > if size < 1.kilobyte > size + " Bytes" > elsif size < 1.megabyte > sprintf("%.0f KB", size / 1.kilobyte.to_f) > elsif size < 1.gigabyte > sprintf("%.1f MB", size / 1.megabyte.to_f) > elsif size < 1.terabyte > sprintf("%.1f GB", size / 1.gigabyte.to_f) > elsif size < 1.petabyte > sprintf("%.1f TB", size / 1.terabyte.to_f) > elsif size < 1.exabyte > sprintf("%.1f PB", size / 1.petabyte.to_f) > else > sprintf("%.1f EB", size / 1.exabyte.to_f) > end > end > > Ryan > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---