Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/core_ext/hash.rb

Overview

Instance Method Summary collapse

Instance Method Details

#except(*keys) ⇒ Object

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))


10
11
12
# File 'lib/openstudio/core_ext/hash.rb', line 10

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

#except!(*keys) ⇒ Object

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 }


18
19
20
21
# File 'lib/openstudio/core_ext/hash.rb', line 18

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