Sha256: 28a822e4d3acb959632e21ea69cb6af5b2a2e966397d2a62077680d4271772c0

Contents?: true

Size: 827 Bytes

Versions: 52

Compression:

Stored size: 827 Bytes

Contents

# frozen_string_literal: true

class Hash
  # Replaces the hash with only the given keys.
  # Returns a hash containing the removed key/value pairs.
  #
  #   hash = { a: 1, b: 2, c: 3, d: 4 }
  #   hash.slice!(:a, :b)  # => {:c=>3, :d=>4}
  #   hash                 # => {:a=>1, :b=>2}
  def slice!(*keys)
    omit = slice(*self.keys - keys)
    hash = slice(*keys)
    hash.default      = default
    hash.default_proc = default_proc if default_proc
    replace(hash)
    omit
  end

  # Removes and returns the key/value pairs matching the given keys.
  #
  #   { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => {:a=>1, :b=>2}
  #   { a: 1, b: 2 }.extract!(:a, :x)             # => {:a=>1}
  def extract!(*keys)
    keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
  end
end

Version data entries

52 entries across 50 versions & 8 rubygems

Version Path
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/core_ext/hash/slice.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.6.1 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.6 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.5.1 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.5 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.4.8 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.4.7 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.4.6 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.4.5 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.4.4 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.4.3 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.4.2 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.4.1 lib/active_support/core_ext/hash/slice.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.4/lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.4 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.3.7 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.3.6 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.3.5 lib/active_support/core_ext/hash/slice.rb
activesupport-6.0.3.4 lib/active_support/core_ext/hash/slice.rb