Sha256: f61c5d0b9aacd425bfc8ff57ff5a0e1d4c2474f99cb9d119ebe562cbf45f2ec8

Contents?: true

Size: 522 Bytes

Versions: 14

Compression:

Stored size: 522 Bytes

Contents

# frozen_string_literal: true

# Extensions to the core Hash class
class Hash
  unless method_defined?(:slice)
    # 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.
    #
    # @return [Hash] hash containing only the keys given.
    #
    # @example
    #   { one: 1, two: 2 }.slice(:two, :three) #=> { two: 2 }
    def slice(*keys)
      h = {}
      keys.each { |k| h[k] = self[k] if key?(k) }
      h
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rubocop-1.12.1 lib/rubocop/core_ext/hash.rb
rubocop-1.12.0 lib/rubocop/core_ext/hash.rb
rubocop-1.11.0 lib/rubocop/core_ext/hash.rb
rubocop-1.10.0 lib/rubocop/core_ext/hash.rb
rubocop-1.9.1 lib/rubocop/core_ext/hash.rb
rubocop-1.9.0 lib/rubocop/core_ext/hash.rb
rubocop-1.8.1 lib/rubocop/core_ext/hash.rb
rubocop-1.8.0 lib/rubocop/core_ext/hash.rb
rubocop-1.7.0 lib/rubocop/core_ext/hash.rb
rubocop-1.6.1 lib/rubocop/core_ext/hash.rb
rubocop-1.6.0 lib/rubocop/core_ext/hash.rb
rubocop-1.5.2 lib/rubocop/core_ext/hash.rb
rubocop-1.5.1 lib/rubocop/core_ext/hash.rb
rubocop-1.5.0 lib/rubocop/core_ext/hash.rb