Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hash.rb

Overview

Extend simple key => string Hash to output nicely formated json for SQL output

Instance Method Summary collapse

Instance Method Details

#to_j(indent = 0) ⇒ String

Adds formatted json convertion

Returns:

  • (String)

    Json for the Hash, and recurse for each value in the Hash



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/hash.rb', line 6

def to_j(indent = 0)
  return "{}\n" if self.length == 0

  s = "{\n"
  self.each do |k, v|
    s += if v.instance_of?(Array) || v.instance_of?(Hash)
           '  ' * (indent + 1) + "\"#{k}\": #{v.to_j(indent + 1)},\n"
         else
           '  ' * (indent + 1) + "\"#{k}\": #{v.to_j},\n"
         end
  end
  s[-2] = ' ' # remove last ,
  s += '  ' * indent + '}'
  return s
end