filesize
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
Usage
Parsing a string
Filesize.from("1 GiB")
Converting filesizes
Filesize.from("1 GiB").to_f('KiB')
Filesize.from("1 GiB").to_f('KB')
Filesize.from("1 GB").to_i
Outputting filesizes
Filesize.from("12502343 B").to_s('GiB')
Filesize.from("12502343 B").pretty
Comparing filesizes
Filesize.from("1 KB") <=> Filesize.from("1 MB")
Calculating with filesizes
The file size on the left side sets the type
(Filesize.from("1400 MB") + Filesize.from("1400 MiB")).pretty
(Filesize.from("1400 MiB") + Filesize.from("1400 MB")).pretty
Filesizes can also be coerced
(Filesize.from("1400 MiB") + 1024).pretty
(1024 + Filesize.from("1400 MB")).pretty
filesize.rb is smart about the return value
Filesize.from("1400 MiB") / Filesize.from("700 MiB")
One can also use predefined sizes
Filesize::DVD / Filesize::CD