Module: Bixby::Debug

Defined in:
lib/bixby-common/util/debug.rb

Class Method Summary collapse

Class Method Details

.indent_lines(str, indent = "\t") ⇒ Object



42
43
44
# File 'lib/bixby-common/util/debug.rb', line 42

def self.indent_lines(str, indent="\t")
  str.gsub(/\n/, "\n#{indent}")
end

.pretty_hash(hash) ⇒ String

Pretty print a hash

Examples:

{
  content-md5   => "Rb0qo2Ae7KGcS5TDulOjYw==",
  date          => "Thu, 29 Aug 2013 13:49:53 GMT",
  authorization => "APIAuth c61ca57b8d7b3e95fba06a",
}

Parameters:

  • hash (Hash)

Returns:

  • (String)


31
32
33
34
35
36
37
38
39
40
# File 'lib/bixby-common/util/debug.rb', line 31

def self.pretty_hash(hash)
  return "{}" if hash.empty?

  s = [ "\n\t{" ]
  l = hash.keys.max_by{ |k| k.length }.length + 1 # length of longest key so we can align values
  hash.keys.each{ |k| s << ("  %s%s=> %s," % [k, " "*(l-k.length), hash[k].inspect]) }
  s << "}"

  return s.join("\n\t")
end

.pretty_str(str) ⇒ Object

Simple helper for use in to_s methods



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bixby-common/util/debug.rb', line 8

def self.pretty_str(str)
  if str.nil? then
    "nil"
  elsif str.empty? then
    '""'
  elsif str.include? "\n" then
    "<<-EOF\n" + str + "\nEOF"
  else
    '"' + str + '"'
  end
end