Module: Diskman::System

Defined in:
lib/diskman/system.rb

Class Method Summary collapse

Class Method Details

.bytes2human(b) ⇒ Object

Convert bytes into a human-friendly representation.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/diskman/system.rb', line 13

def self.bytes2human(b)
    return '0B' if b <= 0

    # Use 1000 to match the misleading way disk capacities are advertised.
    k = 1000

    suffixes = %w[T G M K B]

    suffixes.each_with_index do |suffix, i|
        threshold = k ** (suffixes.length - i - 1)
        if b >= threshold
            return (b / threshold).to_s + suffix
        end
    end
end

.exec!(cmd, safe: true) ⇒ Object

Execute a command.

If safe is true, ensures the user definitely wants to run the command before running it.



6
7
8
9
10
# File 'lib/diskman/system.rb', line 6

def self.exec!(cmd, safe: true)
    Confirmer.check! if safe
    puts
    exec(cmd)
end