File: sample_json.rb

package info (click to toggle)
ruby-oj 2.17.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 968 kB
  • ctags: 1,672
  • sloc: ansic: 10,658; ruby: 6,524; makefile: 2
file content (37 lines) | stat: -rwxr-xr-x 735 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env ruby -wW2

if $0 == __FILE__
  $: << '.'
  $: << '..'
  $: << '../lib'
  $: << '../ext'
end

require 'pp'
require 'oj'

def sample_json(size=3)
  colors = [ :black, :gray, :white, :red, :blue, :yellow, :green, :purple, :orange ]
  container = []
  size.times do |i|
    box = {
      'color' => colors[i % colors.size],
      'fragile' => (0 == (i % 2)),
      'width' => i,
      'height' => i,
      'depth' => i,
      'weight' => i * 1.3,
      'address' => {
        'street' => "#{i} Main Street",
        'city' => 'Sity',
        'state' => nil
      }
    }
    container << box
  end
  container
end

if $0 == __FILE__
  File.open('sample.json', "w") { |f| f.write(Oj.dump(sample_json(3), :indent => 2)) }
end