Skip to content

Commit

Permalink
Merge pull request #17543 from rishijain/update_docs_9
Browse files Browse the repository at this point in the history
added example of hash#except, and removed extra whitespaces [ci skip]
  • Loading branch information
rafaelfranca committed Nov 6, 2014
2 parents 300c96c + c0357d7 commit 001b270
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions activesupport/lib/active_support/core_ext/hash/compact.rb
@@ -1,16 +1,16 @@
class Hash
# Returns a hash with non +nil+ values.
#
#
# hash = { a: true, b: false, c: nil}
# hash.compact # => { a: true, b: false}
# hash # => { a: true, b: false, c: nil}
# { c: nil }.compact # => {}
def compact
self.select { |_, value| !value.nil? }
end

# Replaces current hash with non +nil+ values.
#
#
# hash = { a: true, b: false, c: nil}
# hash.compact! # => { a: true, b: false}
# hash # => { a: true, b: false}
Expand Down
10 changes: 8 additions & 2 deletions activesupport/lib/active_support/core_ext/hash/except.rb
@@ -1,13 +1,19 @@
class Hash
# Returns a hash that includes everything but the given keys. This is useful for
# limiting a set of parameters to everything but a few known toggles:
# Returns a hash that includes everything but the given keys.
# hash = { a: true, b: false, c: nil}
# hash.except(:c) # => { a: true, b: false}
# hash # => { a: true, b: false, c: nil}
#
# This is useful for limiting a set of parameters to everything but a few known toggles:
# @person.update(params[:person].except(:admin))
def except(*keys)
dup.except!(*keys)
end

# Replaces the hash without the given keys.
# hash = { a: true, b: false, c: nil}
# hash.except!(:c) # => { a: true, b: false}
# hash # => { a: true, b: false }
def except!(*keys)
keys.each { |key| delete(key) }
self
Expand Down

0 comments on commit 001b270

Please sign in to comment.