Removing all empty elements from a hash / YAML?

前端 未结 20 1699
礼貌的吻别
礼貌的吻别 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条回答
  •  旧时难觅i
    2020-12-07 15:58

    If you're using Ruby 2.4+, you can call compact and compact!

    h = { a: 1, b: false, c: nil }
    h.compact! #=> { a: 1, b: false }
    

    https://ruby-doc.org/core-2.4.0/Hash.html#method-i-compact-21

提交回复
热议问题