title: Remove nil values from hash
body: <p>I am looking to remove keys from hash that have <code>nil</code> value. <code>article</code> is a class storing each article, and <code>attributes</code> method stores the article as hash.</p>
  
  <p>Expected Result:</p>
  
  <pre><code>{"articles":[{"results":[{"author":null,"title":"Former bar manager jailed for preying on homeless 14-year-old girl","summary":"&lt;p&gt;&lt;img src=\"http://images.theage.com.au/2015/08/24/6790912/Thumbnail999662740gisd08image.related.thumbnail.320x214.gj68pg.png1440386418031.jpg-90x60.jpg\" width=\"90\" height=\"60\" style=\"float:left;margin:4px;border:0px\"/&gt;&lt;/p&gt;A man who preyed on a 14-year-old girl he came across living on the streets of&amp;#160;Wodonga has been jailed for nine months.","images":null,"source":null,"date":"Mon, 24 Aug 2015 03:20:21 +0000","guid":"&lt;guid isPermaLink=\"false\"&gt;gj68pg&lt;/guid&gt;","link":"http://www.theage.com.au/victoria/former-bar-manager-jailed-for-preying-on-homeless-14yearold-girl-20150824-gj68pg.html","section":null,"item_type":null,"updated_date":null,"created_date":null,"material_type_facet":null,"abstract":null,"byline":null,"kicker":null}]}]}
  </code></pre>
  
  <p>Looking to remove null values from the above output.</p>
  
  <pre><code>def attributes
    hash = {
      "author" =&gt; @author,
      "title" =&gt; @title,
      "summary" =&gt; @summary,
      "images" =&gt; @images,
      "source" =&gt; @source,
      "date" =&gt; @date
    }
    hash = {}
    count = 0
    article.attributes.each do |key,value|
      if value == nil
        hash[count] = article.attributes.delete(key)
        count += 1
      end
    end
    hash.to_json
  </code></pre>
  
  <p>The result is as below:</p>
  
  <pre><code>{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null,"9":null,"10":null}
  </code></pre>