mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
added example of hash#except, and removed extra whitespaces [ci skip]
This commit is contained in:
parent
300c96ce15
commit
c0357d789b
2 changed files with 11 additions and 5 deletions
|
@ -1,6 +1,6 @@
|
|||
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}
|
||||
|
@ -8,9 +8,9 @@ class Hash
|
|||
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}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue