小编cod*_*r05的帖子

从散列中删除nil值

我希望从散列中删除nil有价值的键.article是存储每篇文章的类,并且attributes方法将文章存储为散列.

预期结果:

{"articles":[{"results":[{"author":null,"title":"Former bar manager jailed for preying on homeless 14-year-old girl","summary":"<p><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\"/></p>A man who preyed on a 14-year-old girl he came across living on the streets of&#160;Wodonga has been jailed for nine months.","images":null,"source":null,"date":"Mon, 24 Aug 2015 03:20:21 +0000","guid":"<guid isPermaLink=\"false\">gj68pg</guid>","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}]}]}
Run Code Online (Sandbox Code Playgroud)

希望从上面的输出中删除空值.

def attributes
  hash = {
    "author" => @author,
    "title" => @title,
    "summary" => @summary,
    "images" => @images,
    "source" => @source,
    "date" => @date
  }
  hash = {}
  count = 0 …
Run Code Online (Sandbox Code Playgroud)

ruby hash json

1
推荐指数
4
解决办法
5943
查看次数

标签 统计

hash ×1

json ×1

ruby ×1