Removing all empty elements from a hash / YAML? Removing all empty elements from a hash / YAML? ruby ruby

Removing all empty elements from a hash / YAML?


Rails 4.1 added Hash#compact and Hash#compact! as a core extensions to Ruby's Hash class. You can use them like this:

hash = { a: true, b: false, c: nil }hash.compact                        # => { a: true, b: false }hash                                # => { a: true, b: false, c: nil }hash.compact!                        # => { a: true, b: false }hash                                # => { a: true, b: false }{ c: nil }.compact                  # => {}

Heads up: this implementation is not recursive. As a curiosity, they implemented it using #select instead of #delete_if for performance reasons. See here for the benchmark.

In case you want to backport it to your Rails 3 app:

# config/initializers/rails4_backports.rbclass Hash  # as implemented in Rails 4  # File activesupport/lib/active_support/core_ext/hash/compact.rb, line 8  def compact    self.select { |_, value| !value.nil? }  endend


Use hsh.delete_if. In your specific case, something like: hsh.delete_if { |k, v| v.empty? }


You could add a compact method to Hash like this

class Hash  def compact    delete_if { |k, v| v.nil? }  endend

or for a version that supports recursion

class Hash  def compact(opts={})    inject({}) do |new_hash, (k,v)|      if !v.nil?        new_hash[k] = opts[:recurse] && v.class == Hash ? v.compact(opts) : v      end      new_hash    end  endend