2 Replies - 3059 Views - Last Post: 19 February 2013 - 07:26 AM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

show files size in kilobytes megabytes or gigabytes, depending

Posted 18 February 2013 - 04:20 PM

I'm working on a gem that backs up files, and I'm wondering what the best way is to derive a human readable "file size" value from a summation like this:

buffer = 0
files.each do |f|
  buffer += File.size(File.expand_path(f))
end

puts buffer.show_pretty_size




I did some searching and saw http://www.ruby-forum.com/topic/92075 where they point out that Action View harbors some code that does that. Until I got about halfway through writing this I couldn't figure out how to use the code (needed include ActionView::Helpers::NumberHelper, doh!). But for a stand along gem (and one not related to rails) is including this gem a fairly reasonable way to go or does that seem too bulky? Is there some code buried away somewhere in the standard ruby library that does it as well perhaps? I'm tempted to just copy and paste the code and move on but I can't decide exactly how I feel about adding complexity (another stray method) vs adding dependencies (adding the action_view gem).

Is This A Good Question/Topic? 0
  • +

Replies To: show files size in kilobytes megabytes or gigabytes, depending

#2 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1369
  • View blog
  • Posts: 3,456
  • Joined: 28-November 09

Re: show files size in kilobytes megabytes or gigabytes, depending

Posted 18 February 2013 - 08:15 PM

Should just be a matter of dividing by 1024 and throwing a case statement in there to interpret it, such as:

buffer.to kb
buffer.to mb
buffer.to gb

def to(type)
 # insert case statements here.
end


As far as something built in, you might read on the file module on the ruby libraries page.

...or this clever bit:

class Numeric
  def to_human
    units = %w{B KB MB GB TB}
    e = (Math.log(self)/Math.log(1024)).floor
    s = "%.3f" % (to_f / 1024**e)
    s.sub(/\.?0*$/, units[e])
  end
end


..found at http://www.ruby-forum.com/topic/126876
Was This Post Helpful? 2
  • +
  • -

#3 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: show files size in kilobytes megabytes or gigabytes, depending

Posted 19 February 2013 - 07:26 AM

Thanks for the response, especially that link, I never encountered that page. I felt better about manually adding the method to my gem after I refactored my code into a separate lib/backup_nix/backup_info.rb file where there's plenty of space (the whole file fits on 1 screen still actually).

I wound up using the second algo on that page, mostly because I found the other solution a little difficult to parse,
# Return the file size with a readable style.
    def readable_file_size(size, precision)
      case
        when size == 1 ; "1 Byte"
        when size < KILO_SIZE ; "%d Bytes" % size
        when size < MEGA_SIZE ; "%.#{precision}f KB" % (size / KILO_SIZE)
        when size < GIGA_SIZE ; "%.#{precision}f MB" % (size / MEGA_SIZE)
        else "%.#{precision}f GB" % (size / GIGA_SIZE)
      end
    end


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1