Class: Size
- Inherits:
-
Object
- Object
- Size
- Defined in:
- lib/types.rb
Overview
A class that represents sizes of directories. The main feature is to give back a size in a “human readable” format, like “5 MiB”.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#b ⇒ Object
Returns the size in bytes as an integer.
-
#initialize(string) ⇒ Size
constructor
Takes a size as a string.
-
#opt(dec = 2) ⇒ Object
Returns a string which is “optimal” formatted, like “34.333 GiB”, rounded to “dec” digits after the comma.
-
#short(dec = 2) ⇒ Object
Returns a string which is “optimal” formatted, but with a short ending, like “34.33g”, rounded to “dec” digits after the comma.
-
#to_s ⇒ Object
Returns the size in bytes as a string.
Constructor Details
#initialize(string) ⇒ Size
Takes a size as a string. With no ending it is assumed as a size in bytes. Otherwise, (for example gvien a “32m”) it is interpreted depending on the ending:
- g
-
GiB
- m
-
MiB
- k
-
KiB
Raises an error if an unexpected string is given (like “m32” or “32 MiB”).
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/types.rb', line 111 def initialize(string) size = string.to_s.strip.downcase case size when "" then @size=0 when /g$/ then @size=$`.to_f * 1024**3 raise "Wrong Size format, '" + string + "' not valid!" unless $`.to_f.to_s == $` or $`.to_i.to_s == $` when /m$/ then @size=$`.to_f * 1024**2 raise "Wrong Size format, '" + string + "' not valid!" unless $`.to_f.to_s == $` or $`.to_i.to_s == $` when /k$/ then @size=$`.to_f * 1024 raise "Wrong Size format, '" + string + "' not valid!" unless $`.to_f.to_s == $` or $`.to_i.to_s == $` else @size=size.to_f raise "Wrong Size format, '" + string + "' not valid!" unless size.to_f.to_s == size or size.to_i.to_s == size end end |
Instance Method Details
#==(other) ⇒ Object
169 170 171 |
# File 'lib/types.rb', line 169 def ==(other) return b == other.b end |
#b ⇒ Object
Returns the size in bytes as an integer.
160 161 162 |
# File 'lib/types.rb', line 160 def b @size.round end |
#opt(dec = 2) ⇒ Object
Returns a string which is “optimal” formatted, like “34.333 GiB”, rounded to “dec” digits after the comma.
Default dec: 2. A dec of “-1” means no rounding.
Uses “B”, “KiB”, “MiB” or “GiB” as ending.
133 134 135 136 137 138 139 140 141 |
# File 'lib/types.rb', line 133 def opt(dec = 2) case @size.abs when 0..1023 then s, d=@size, " B" when 1024..1024**2-1 then s, d=@size/1024, " KiB" when 1024**2..1024**3-1 then s, d=@size/1024**2, " MiB" else s, d=@size/1024**3, " GiB" end return dec==-1 ? s.to_s + d : ((s * 10**dec).round.to_f / 10**dec).to_s + d end |
#short(dec = 2) ⇒ Object
Returns a string which is “optimal” formatted, but with a short ending, like “34.33g”, rounded to “dec” digits after the comma.
Default dec: 2. A dec of “-1” means no rounding.
Uses “”, “k”, “m” or “g” as ending.
149 150 151 152 153 154 155 156 157 |
# File 'lib/types.rb', line 149 def short(dec = 2) case @size.abs when 0..1023 then s, d=@size, "" when 1024..1024**2-1 then s, d=@size.to_f/1024, "k" when 1024**2..1024**3-1 then s, d=@size.to_f/1024**2, "m" else s, d=@size.to_f/1024**3, "g" end return dec==-1 ? s.to_s + d : ((s * 10**dec).round.to_f / 10**dec).to_s + d end |
#to_s ⇒ Object
Returns the size in bytes as a string.
165 166 167 |
# File 'lib/types.rb', line 165 def to_s return @size.to_s end |