Skip to content

Instantly share code, notes, and snippets.

@jschoolcraft
Created October 17, 2009 03:08
Show Gist options
  • Select an option

  • Save jschoolcraft/212220 to your computer and use it in GitHub Desktop.

Select an option

Save jschoolcraft/212220 to your computer and use it in GitHub Desktop.
##
# rSpec Hash additions.
#
# From
# * http://wincent.com/knowledge-base/Fixtures_considered_harmful%3F
# * Neil Rahilly
class Hash
##
# Filter keys out of a Hash.
#
# { :a => 1, :b => 2, :c => 3 }.except(:a)
# => { :b => 2, :c => 3 }
def except(*keys)
self.reject { |k,v| keys.include?(k || k.to_sym) }
end
##
# Override some keys.
#
# { :a => 1, :b => 2, :c => 3 }.with(:a => 4)
# => { :a => 4, :b => 2, :c => 3 }
def with(overrides = {})
self.merge overrides
end
##
# Returns a Hash with only the pairs identified by +keys+.
#
# { :a => 1, :b => 2, :c => 3 }.only(:a)
# => { :a => 1 }
def only(*keys)
self.reject { |k,v| !keys.include?(k || k.to_sym) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment