Git Product home page Git Product logo

filesize's Introduction

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")
# => #<Filesize:0x93c06c8 @bytes=1073741824, @type={:regexp=>/^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i, :multiplier=>1024, :presuffix=>"i"}>

Converting filesizes

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

Filesize.from("12502343 B").to_s('GiB') # => "0.01 GiB"
Filesize.from("12502343 B").pretty      # => "11.92 MiB"

Comparing filesizes

Filesize.from("1 KB") <=> Filesize.from("1 MB") # => -1

Calculating with filesizes

The file size on the left side sets the type

(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

(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

Filesize.from("1400 MiB") / Filesize.from("700 MiB") # => 2.0

One can also use predefined sizes

Filesize::DVD / Filesize::CD # => 6.13566756571429

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.