title: How to recursively remove all keys with empty values from (YAML) hash?
body: <p>I have been trying to get rid of all hash keys in my YAML file that have empty (blank) values or empty hashes as values.</p>
  
  <p>This <a href="https://stackoverflow.com/questions/3450641/ruby-removing-all-empty-elements-from-a-hash-yaml">earlier post</a> helped me to get it almost right, but the recursive one-liner leaves my YAML dump with empty hashes whenever there is sufficiently deep nesting.</p>
  
  <p>I would really appreciate any help on this. Thanks!</p>
  
  <pre><code>proc = Proc.new { |k, v| (v.kind_of?(Hash) &amp;&amp; !v.empty? ) ? (v.delete_if(&amp;proc); nil) : v.blank? }
  
  hash = {"x"=&gt;{"m"=&gt;{"n"=&gt;{}}}, 'y' =&gt; 'content'}
  hash.delete_if(&amp;proc)
  </code></pre>
  
  <p>Actual output</p>
  
  <pre><code> {"x"=&gt;{"m"=&gt;{}}, "y"=&gt;"content"} 
  </code></pre>
  
  <p>Desired output</p>
  
  <pre><code>{"y"=&gt;"content"}
  </code></pre>