collectiveidea / awesomeness

Collective Idea's Awesomeness. A collection of useful Rails bits and pieces.

This URL has Read+Write access

awesomeness / lib / awesomeness / core_ext / hash.rb
100644 20 lines (18 sloc) 0.576 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Hash
  # Usage {:a => [1,3,4], :b => [2,5]}.without(:a => 1)
  # returns {:a => [3,4], :b => [2,5]}
  def without(hash)
    hash = hash.with_indifferent_access if self.instance_of?(HashWithIndifferentAccess)
    # create a new hash using this class, so we get a new HashWithIndifferentAccess if this is one
    inject(self.class.new) do |result,(k,v)|
      result[k] = (hash[k] && v.respond_to?(:reject) ? v.reject {|v,_| v == hash[k] } : v)
      result
    end
  end
  
  def compact!
    delete_if { |k,v| v.blank? }
  end
  
  def compact
    dup.compact!
  end
end