Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/rbfind/humansiz.rb,
lib/rbfind/humansiz.rb

Overview

sizes in bytes

Constant Summary collapse

K =

:stopdoc:

1000
M =
K * K
G =
K * M
T =
K * G
Kb =

:stopdoc:

1024
Mb =
Kb * Kb
Gb =
Kb * Mb
Tb =
Kb * Gb
PREFIXES =

:nodoc:

" kMGTPEZY".scan /./

Instance Method Summary collapse

Instance Method Details

#GBObject



35
# File 'lib/rbfind/humansiz.rb', line 35

def GB ; self * G ; end

#GiBObject



48
# File 'lib/rbfind/humansiz.rb', line 48

def GiB ; self * Gb ; end

#kBObject

:startdoc:



33
# File 'lib/rbfind/humansiz.rb', line 33

def kB ; self * K ; end

#kiBObject

:startdoc:



46
# File 'lib/rbfind/humansiz.rb', line 46

def kiB ; self * Kb ; end

#MBObject



34
# File 'lib/rbfind/humansiz.rb', line 34

def MB ; self * M ; end

#MiBObject



47
# File 'lib/rbfind/humansiz.rb', line 47

def MiB ; self * Mb ; end

#tObject



100
# File 'lib/rbfind/humansiz.rb', line 100

def t ; Time.to_unit to_i ; end

#TBObject



36
# File 'lib/rbfind/humansiz.rb', line 36

def TB ; self * T ; end

#TiBObject



49
# File 'lib/rbfind/humansiz.rb', line 49

def TiB ; self * Tb ; end

#to_hObject

:call-seq:

num.to_h()  -> str

To human readable with decimal prefixes.

4096.to_h   #=> "  4.1kB"


60
61
62
63
64
65
66
# File 'lib/rbfind/humansiz.rb', line 60

def to_h
  n = 0
  s = to_f
  while s >= K do s /= K ; n += 1 end
  format = n.zero? ? "%3d  " : "%5.1f"
  (format % s) + (PREFIXES[ n]||"?") + "B"
end

#to_hibObject

:call-seq:

num.to_hib()  -> str

To human readable with binary prefixes.

4096.to_hib   #=> "   4.0kiB"


75
76
77
78
79
80
81
# File 'lib/rbfind/humansiz.rb', line 75

def to_hib
  n = 0
  s = to_f
  while s >= Kb do s /= Kb ; n += 1 end
  format = n.zero? ? "%4d  " : "%6.1f"
  (format % s) + (PREFIXES[ n]||"?") + "iB"
end