Module: Shepherd::Utils

Included in:
Shepherd
Defined in:
lib/shepherd/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.nice_bytes(number) ⇒ String

Convert bytes into more human readable format. I have found nice resolution here

which was originally written by Jeff Emminger and I just add this part
to automaticly set the unit. Thank you, Jeff!

Parameters:

  • number (Integer)

    of bytes to be formatted

Returns:

  • (String)

    a formatted number with the unit appended to it



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shepherd/utils.rb', line 24

def self.nice_bytes number
  units = {:b => 1,
        :kb => 2**10,
        :mb => 2**20,
        :gb => 2**30,
        :tb => 2**40}
  
  unit = :b
  case number
    when 1...2**10
      unit = :b
    when 2**10...2**20
      unit = :kb
    when 2**20...2**30
      unit = :mb
    when 2**30...2**40
      unit = :gb
    when 2**40...2**50
      unit = :tb
  end
  "#{sprintf("%.#{0}f", number / units[unit.to_s.downcase.to_sym])} #{unit.to_s.upcase}"
end

Instance Method Details

#yes?(msg = "Would you like to proceed?") ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/shepherd/utils.rb', line 3

def yes? msg = "Would you like to proceed?"
  print "#{msg} [Yn]: "
  case $stdin.gets
  when /(y|yes)/i
    true
  when "\n"
    true
  when /(n|no)/i
    false
  else
    false
  end
end