Class: Array

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

Overview

Extend Array to output nicely formated json.

Instance Method Summary collapse

Instance Method Details

#to_j(indent = 0) ⇒ String

Returns Json array.

Returns:

  • Json array



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/array.rb', line 4

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