Class: Benchmark::Memory::HumanReadableUnit

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/benchmark/memory/human_readable_unit.rb

Overview

Transforms raw numbers into a human-readable scale and suffix

Instance Method Summary collapse

Instance Method Details

#scaleInteger

Returns the exponential scale of the value.

Returns:

  • (Integer)

    the exponential scale of the value



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/benchmark/memory/human_readable_unit.rb', line 10

def scale
  scale = Math.log10(__getobj__)
  scale = 0 if scale.infinite?
  scale = (scale / 3).to_i

  if scale <= 5
    scale
  else
    0
  end
end

#unitString

Returns the single-character unit for the value.

Returns:

  • (String)

    the single-character unit for the value



23
24
25
26
27
28
29
30
31
32
# File 'lib/benchmark/memory/human_readable_unit.rb', line 23

def unit
  case scale
  when 1 then 'k'
  when 2 then 'M'
  when 3 then 'B'
  when 4 then 'T'
  when 5 then 'Q'
  else ' '
  end
end