class Filesize
Constants
- BINARY
Set of rules describing file sizes according to binary units.
- CD
The size of a
CD
- DVD
The same as a
DVD
5- DVD_10
The size of a double-sided single-layer
DVD
- DVD_14
The size of a double-sided
DVD
, combining a DVD-9 and a DVD-5- DVD_18
The size of a double-sided dual-layer
DVD
- DVD_5
The size of a common
DVD
- DVD_9
The size of a single-sided dual-layer
DVD
- Floppy
The size of a floppy disk
- PREFIXES
@deprecated Please use TYPE_PREFIXES instead
- SI
Set of rules describing file sizes according to
SI
units.- TYPE_PREFIXES
- ZIP
The size of a Zip disk
Public Class Methods
Parses a string, which describes a file size, and returns a Filesize
object.
@param [String] arg A file size to parse. @raise [ArgumentError] Raised if the file size cannot be parsed properly. @return [Filesize]
# File lib/filesize.rb, line 129 def from(arg) parts = parse(arg) prefix = parts[:prefix] size = parts[:size] type = parts[:type] raise ArgumentError, "Unparseable filesize" unless type offset = (type[:prefixes].map { |s| s[0].chr.downcase }.index(prefix.downcase) || -1) + 1 new(size * (type[:multiplier] ** (offset)), type) end
@param [Number] size A file size, in bytes. @param [SI, BINARY] type Which type to use for conversions.
# File lib/filesize.rb, line 31 def initialize(size, type = BINARY) @bytes = size.to_i @type = type end
@return [Hash<:prefix, :size, :type>] @api private
# File lib/filesize.rb, line 144 def parse(string) type = nil # in this order, so we prefer binary :) [BINARY, SI].each { |_type| if string =~ _type[:regexp] type = _type break end } prefix = $2 || '' size = ($1 || 0).to_f return { :prefix => prefix, :size => size, :type => type} end
Public Instance Methods
@return [Filesize]
# File lib/filesize.rb, line 98 def *(other) self.class.new(@bytes * other.to_i, @type) end
@return [Filesize]
# File lib/filesize.rb, line 88 def +(other) self.class.new(@bytes + other.to_i, @type) end
@return [Filesize]
# File lib/filesize.rb, line 93 def -(other) self.class.new(@bytes - other.to_i, @type) end
@return [Filesize]
# File lib/filesize.rb, line 103 def /(other) result = @bytes / other.to_f if other.is_a? Filesize result else self.class.new(result, @type) end end
# File lib/filesize.rb, line 112 def <=>(other) self.to_i <=> other.to_i end
@return [Array<self, other>] @api private
# File lib/filesize.rb, line 118 def coerce(other) return self, other end
Same as {#to_s} but with an automatic determination of the most sensible unit.
@return [String] @see to_s
# File lib/filesize.rb, line 73 def pretty size = @bytes if size < @type[:multiplier] unit = "B" else pos = (Math.log(size) / Math.log(@type[:multiplier])).floor pos = @type[:prefixes].size-1 if pos > @type[:prefixes].size - 1 unit = @type[:prefixes][pos-1] + "B" end to_s(unit) end
@param [String] unit Which unit to convert to. @return [Float] Returns the size in a given unit.
# File lib/filesize.rb, line 44 def to(unit = 'B') to_parts = self.class.parse(unit) prefix = to_parts[:prefix] if prefix == 'B' or prefix.empty? return to_i.to_f end to_type = to_parts[:type] size = @bytes pos = (@type[:prefixes].map { |s| s[0].chr.downcase }.index(prefix.downcase) || -1) + 1 size = size/(to_type[:multiplier].to_f**(pos)) unless pos < 1 end
@return [Number] Returns the size in bytes.
# File lib/filesize.rb, line 37 def to_i @bytes end