class Hash

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

Public Instance Methods

to_j(indent=0) click to toggle source

@return [String] Json for the Hash, and recurse for each value in the Hash

# File lib/hash.rb, line 4
def to_j(indent=0)
  return "{}\n" if self.length == 0
  s = "{\n"
    self.each do |k,v| 
      if v.class == Array || v.class == Hash 
        s += "  "*(indent+1) + "\"#{k}\": #{v.to_j(indent+1)},\n"
      else
        s += "  "*(indent+1) + "\"#{k}\": #{v.to_j},\n"
      end
  end
  s[-2] = " " #remove last ,
  s += "  "*indent + "}"
  return s
end