Class: Hash

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

Overview

Extensions to the core Hash class

Instance Method Summary collapse

Instance Method Details

#slice(*keys) ⇒ Hash

Adds `Hash#slice` for Ruby 2.4. Returns a hash containing a subset of keys. If a given key is not in the hash, it will not be returned.

Examples:

{ one: 1, two: 2 }.slice(:two, :three) #=> { two: 2 }

Returns:

  • (Hash)

    hash containing only the keys given.


14
15
16
17
18
# File 'lib/rubocop/core_ext/hash.rb', line 14

def slice(*keys)
  h = {}
  keys.each { |k| h[k] = self[k] if key?(k) }
  h
end