title: Printing printing JSON in Ruby - with line wrapping
body: <p>What is the best way to pretty print JSON in Ruby <strong>with smart line wrapping</strong>, the way the "json" output formatter in <a href="https://github.com/ddopson/underscore-cli" rel="nofollow">underscore-cli</a> handles it?</p>
  
  <p>For instance, a regular JSON pretty printer would output:</p>
  
  <pre><code>{
    "a": [
      "b",
      "c"
    ],
    "d": {
      "e": "f"
    },
    "g": [
      "some longer string"
    ]
  }
  </code></pre>
  
  <p>But I'm looking for a configurable pretty printer in Ruby that, like underscore-cli, notices that small structures make for pointlessly short lines, and so outputs something like this:</p>
  
  <pre><code>{
    "a": [ "b", "c" ],
    "d": { "e": "f" },
    "g": [ "some longer string" ]
  }
  </code></pre>
  
  <p>I've played around with the options available in <code>JSON.generate()</code> (space, space_before, etc.), to no avail.</p>
  
  <p>I'm writing a script which generates multiple JSON files intended to be reasonably comprehensible by humans (should the need arise) --- I can't expect underscore to be available everywhere so I can't (and ugh, wouldn't want to) just pipe the output through underscore, but the default <code>JSON.pretty_generate()</code> outputs files which are far less readable with around three times as many lines as underscore (2,200 lines vs 750).</p>
  
  <p>Ruby 2.0.0p481.</p>