filesize.rb provides a class for easily working with file sizes. That means:
- Parsing strings (e.g. "1 GiB") and saving it internally as bytes
- Handling both SI and binary prefixes
- Converting from any type and unit to any other (SI to SI, SI to Binary and so on)
- doing calculations with filesizes (in a smart way, see Usage for more)
- filesize.rb also provides some default sizes, like the ones of DVDs
Filesize.parse("1 GiB")
# => #<Filesize:0x93c06c8 @bytes=1073741824, @type={:regexp=>/^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i, :multiplier=>1024, :presuffix=>"i"}>Filesize.parse("1 GiB").to_f('KiB') # => 1048576.0
Filesize.parse("1 GiB").to_f('KB') # => 1073741.824
Filesize.parse("1 GB").to_i # => 1000000000Filesize.parse("12502343 B").to_s # => "11.92 MiB"Filesize.parse("1 KB") <=> Filesize.parse("1 MB") # => -1(Filesize.parse("1400 MB") + Filesize.parse("1400 MiB")).to_s # => "2.87 GB"
(Filesize.parse("1400 MiB") + Filesize.parse("1400 MB")).to_s # => "2.67 GiB"(Filesize.parse("1400 MiB") + 1024).to_s # => "1.37 GiB"
(1024 + Filesize.parse("1400 MB")).to_s # => "1.40 GB"Filesize.parse("1400 MiB") / Filesize.parse("700 MiB") # => 2.0Filesize::DVD / Filesize::CD # => 6.13566756571429