Module: Fusu::Hash::Except

Included in:
Fusu::Hash
Defined in:
lib/fusu/hash/except.rb

Instance Method Summary collapse

Instance Method Details

#except(hash, *keys) ⇒ Object

Returns a hash that includes everything but the given keys.

hash = { a: true, b: false, c: nil}
Fusu::Hash.except(hash, :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(Fusu::Hash.except(params[:person], :admin))


11
12
13
# File 'lib/fusu/hash/except.rb', line 11

def except(hash, *keys)
  except!(hash.dup, *keys)
end

#except!(hash, *keys) ⇒ Object

Replaces the hash without the given keys.

hash = { a: true, b: false, c: nil}
Fusu::Hash.except!(hash, :c) # => { a: true, b: false}
hash # => { a: true, b: false }


19
20
21
22
# File 'lib/fusu/hash/except.rb', line 19

def except!(hash, *keys)
  keys.each { |key| hash.delete(key) }
  hash
end