Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dominikh/filesize
filesize is a small ruby class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.
https://github.com/dominikh/filesize
Last synced: 5 days ago
JSON representation
filesize is a small ruby class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.
- Host: GitHub
- URL: https://github.com/dominikh/filesize
- Owner: dominikh
- License: mit
- Archived: true
- Created: 2009-07-26T07:50:32.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2018-09-04T16:46:36.000Z (about 6 years ago)
- Last Synced: 2024-10-03T01:18:34.325Z (about 1 month ago)
- Language: Ruby
- Homepage:
- Size: 23.4 KB
- Stars: 81
- Watchers: 6
- Forks: 16
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
**This gem is no longer maintained.**
## 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
```ruby
Filesize.from("1 GiB")
# => #/^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i, :multiplier=>1024, :presuffix=>"i"}>
```### Converting filesizes
```ruby
Filesize.from("1 GiB").to_f('KiB') # => 1048576.0
Filesize.from("1 GiB").to_f('KB') # => 1073741.824
Filesize.from("1 GB").to_i # => 1000000000
```### Outputting filesizes
```ruby
Filesize.from("12502343 B").to_s('GiB') # => "0.01 GiB"
Filesize.from("12502343 B").pretty # => "11.92 MiB"
```### Comparing filesizes
```ruby
Filesize.from("1 KB") <=> Filesize.from("1 MB") # => -1
```### Calculating with filesizes
#### The file size on the left side sets the type
```ruby
(Filesize.from("1400 MB") + Filesize.from("1400 MiB")).pretty # => "2.87 GB"
(Filesize.from("1400 MiB") + Filesize.from("1400 MB")).pretty # => "2.67 GiB"
```#### Filesizes can also be coerced
```ruby
(Filesize.from("1400 MiB") + 1024).pretty # => "1.37 GiB"
(1024 + Filesize.from("1400 MB")).pretty # => "1.40 GB"
```#### filesize.rb is smart about the return value
```ruby
Filesize.from("1400 MiB") / Filesize.from("700 MiB") # => 2.0
```#### One can also use predefined sizes
```ruby
Filesize::DVD / Filesize::CD # => 6.13566756571429
```