public
Clone URL: git://github.com/robbyrussell/rubyurl.git
Search Repo:
robbyrussell (author)
Thu Dec 13 16:47:36 -0800 2007
commit  6f261e7b85c2e5f308d2f1dc46907c7237182a17
tree    170cbaf39692356c1da5ad17c2b919cd358aca2a
parent  0ecb284c046b2703a16b08ca6c6fc7812baea967
rubyurl / lib / hash_extensions.rb
100644 19 lines (17 sloc) 0.66 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#
# Based on http://pastie.caboo.se/10707
#
 
class Hash
  # Returns the hash with entries removed
  # { :a => 1, :b => 2, :c => 3}.except(:a) -> { :b => 2, :c => 3 }
  # { :a => 1, :b => 2, :c => 3}.except(:a, :c) -> { :b => 2 }
  def except(*keys)
    self.reject { |k,v| keys.include? k.to_sym }
  end unless instance_methods.include? 'except'
  
  # Returns a new hash with only the entries specified
  # { :a => 1, :b => 2, :c => 3}.only(:a) -> { :a => 1 }
  # { :a => 1, :b => 2, :c => 3}.only(:a, :b) -> { :a => 1, :b => 2 }
  def only(*keys)
    self.dup.reject { |k,v| !keys.include? k.to_sym }
  end unless instance_methods.include? 'only'
end