File: helpers.rb

package info (click to toggle)
ruby-benchmark-memory 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: ruby: 1,121; makefile: 7; sh: 4
file content (37 lines) | stat: -rw-r--r-- 964 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

require 'benchmark/memory/human_readable_unit'

module Benchmark
  module Memory
    # Helper methods for formatting output.
    module Helpers
      # Right-justifies to a length of 20 or adds a line of padding when longer.
      #
      # @param label [#to_s] The label to justify.
      #
      # @return [String] The justified label.
      def rjust(label)
        label = label.to_s

        if label.size > 20
          "#{label}\n#{' ' * 20}"
        else
          label.rjust(20)
        end
      end

      # Scale a value into human-understandable terms.
      #
      # @param value [Integer, Float] The value to scale.
      #
      # @return [String] The scaled value.
      def scale(value)
        value = HumanReadableUnit.new(value)

        format("%10.3f#{value.unit}", value.to_f / (1000**value.scale))
      end
      module_function :scale # rubocop:disable Style/AccessModifierDeclarations
    end
  end
end