public
Description: Your favorite URL-shortening service in all of Ruby land
Homepage: http://rubyurl.com
Clone URL: git://github.com/robbyrussell/rubyurl.git
robbyrussell (author)
Thu Apr 10 13:30:23 -0700 2008
commit  0c493413ec32f4455cad312d9cacf77ec24b4a67
tree    d741363b76a69f8ae49d0442bb7b747344553aba
parent  fc76e8be89cbcc075a8d07e211cc3f64007c319b
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