Class: Filesize

Inherits:
Object
  • Object
show all
Defined in:
lib/filesize.rb

Constant Summary collapse

TYPE_PREFIXES =
{
  # Unit prefixes used for SI file sizes.
  SI: %w{k M G T P E Z Y},
  # Unit prefixes used for binary file sizes.
  BINARY: %w{Ki Mi Gi Ti Pi Ei Zi Yi}
}
PREFIXES =
Deprecated.

Please use TYPE_PREFIXES instead

TYPE_PREFIXES[:SI]
SI =

Set of rules describing file sizes according to SI units.

{
  :regexp => /^([\d,.]+)?\s?([kmgtpezy]?)b$/i,
  :multiplier => 1000,
  :prefixes => TYPE_PREFIXES[:SI],
  :presuffix => '' # deprecated
}
BINARY =

Set of rules describing file sizes according to binary units.

{
  :regexp => /^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i,
  :multiplier => 1024,
  :prefixes => TYPE_PREFIXES[:BINARY],
  :presuffix => 'i' # deprecated
}
Floppy =

The size of a floppy disk

Filesize.from("1474 KiB")
CD =

The size of a CD

Filesize.from("700 MB")
DVD_5 =

The size of a common DVD

Filesize.from("4.38 GiB")
DVD =

The same as a DVD 5

DVD_5
DVD_9 =

The size of a single-sided dual-layer DVD

Filesize.from("7.92 GiB")
DVD_10 =

The size of a double-sided single-layer DVD

DVD_5 * 2
DVD_14 =

The size of a double-sided DVD, combining a DVD-9 and a DVD-5

DVD_9 + DVD_5
DVD_18 =

The size of a double-sided dual-layer DVD

DVD_14 * 2
ZIP =

The size of a Zip disk

Filesize.from("100 MB")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, type = BINARY) ⇒ Filesize



29
30
31
32
# File 'lib/filesize.rb', line 29

def initialize(size, type = BINARY)
  @bytes = size.to_i
  @type  = type
end

Class Method Details

.from(arg) ⇒ Filesize

Parses a string, which describes a file size, and returns a Filesize object.

Raises:

  • (ArgumentError)

    Raised if the file size cannot be parsed properly.



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/filesize.rb', line 128

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].downcase }.index(prefix.downcase) || -1) + 1

  new(size * (type[:multiplier] ** (offset)), type)
end

.parse(string) ⇒ Hash<:prefix, :size, :type>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/filesize.rb', line 143

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

Instance Method Details

#*(other) ⇒ Filesize



96
97
98
# File 'lib/filesize.rb', line 96

def *(other)
  self.class.new(@bytes * other.to_i, @type)
end

#+(other) ⇒ Filesize



86
87
88
# File 'lib/filesize.rb', line 86

def +(other)
  self.class.new(@bytes + other.to_i, @type)
end

#-(other) ⇒ Filesize



91
92
93
# File 'lib/filesize.rb', line 91

def -(other)
  self.class.new(@bytes - other.to_i, @type)
end

#/(other) ⇒ Filesize



101
102
103
104
105
106
107
108
# File 'lib/filesize.rb', line 101

def /(other)
  result = @bytes / other.to_f
  if other.is_a? Filesize
    result
  else
    self.class.new(result, @type)
  end
end

#==(other) ⇒ Boolean



111
112
113
# File 'lib/filesize.rb', line 111

def ==(other)
  other.is_a?(self.class) && other.to_i == self.to_i
end

#coerce(other) ⇒ Array<self, other>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



117
118
119
# File 'lib/filesize.rb', line 117

def coerce(other)
  return self, other
end

#prettyString

Same as #to_s but with an automatic determination of the most sensible unit.

See Also:



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/filesize.rb', line 71

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

#to(unit = 'B') ⇒ Float Also known as: to_f



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/filesize.rb', line 42

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].downcase }.index(prefix.downcase) || -1) + 1

  size = size/(to_type[:multiplier].to_f**(pos)) unless pos < 1
end

#to_iNumber Also known as: to_int



35
36
37
# File 'lib/filesize.rb', line 35

def to_i
  @bytes
end

#to_s(unit = 'B') ⇒ String

Returns Same as #to_f, but as a string, with the unit appended.

See Also:



62
63
64
# File 'lib/filesize.rb', line 62

def to_s(unit = 'B')
  "%.2f %s" % [to(unit).to_f.to_s, unit]
end