public
Clone URL: git://github.com/robbyrussell/rubyurl.git
Search Repo:
rmm5t (author)
Sun Mar 02 14:42:29 -0800 2008
commit  26886b6a75f2c37e6a056058e490e23b04054dcf
tree    32683fd7490fde843e3186728d5791158b9e17b3
parent  20157bf6ac66a72928a455a935a6e8e56bf6110e
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