Removing all empty elements from a hash / YAML?

前端 未结 20 1653
礼貌的吻别
礼貌的吻别 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

    Try this to remove nil

    hash = { a: true, b: false, c: nil }
    => {:a=>true, :b=>false, :c=>nil}
    hash.inject({}){|c, (k, v)| c[k] = v unless v.nil?; c}
    => {:a=>true, :b=>false}
    

提交回复
热议问题