Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/ruby/hash/slice.rb,
lib/core_ext/ruby/hash/except.rb

Instance Method Summary collapse

Instance Method Details

#except(*keys) ⇒ Object



8
9
10
# File 'lib/core_ext/ruby/hash/except.rb', line 8

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

#except!(*keys) ⇒ Object



2
3
4
5
6
# File 'lib/core_ext/ruby/hash/except.rb', line 2

def except!(*keys)
  keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
  keys.each { |key| delete(key) }
  self
end

#slice(*keys) ⇒ Object



9
10
11
12
13
# File 'lib/core_ext/ruby/hash/slice.rb', line 9

def slice(*keys)
  hash = self.class.new
  keys.each { |k| hash[k] = self[k] if has_key?(k) }
  hash
end

#slice!(*keys) ⇒ Object



2
3
4
5
6
7
# File 'lib/core_ext/ruby/hash/slice.rb', line 2

def slice!(*keys)
  omit = slice(*self.keys - keys)
  hash = slice(*keys)
  replace(hash)
  omit
end