Take the 2008 Git User's Survey and help out! [ hide ]

public
Description: Your favorite URL-shortening service in all of Ruby land
Homepage: http://rubyurl.com
Clone URL: git://github.com/robbyrussell/rubyurl.git
Search Repo:
robbyrussell (author)
Thu Dec 13 08:58:39 -0800 2007
commit  0ecb284c046b2703a16b08ca6c6fc7812baea967
tree    f344f47dd4fd9d7455b12a6544656bd852280fcb
parent  e4b44cf959662efeb072aaf66a0975c6e4585a04
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