Removing all empty elements from a hash / YAML?

前端 未结 20 1639
礼貌的吻别
礼貌的吻别 2020-12-07 15:35

How would I go about removing all empty elements (empty list items) from a nested Hash or YAML file?

20条回答
  •  执笔经年
    2020-12-07 15:48

    Could be done with facets library (a missing features from standard library), like that:

    require 'hash/compact'
    require 'enumerable/recursively'
    hash.recursively { |v| v.compact! }
    

    Works with any Enumerable (including Array, Hash).

    Look how recursively method is implemented.

提交回复
热议问题