Sha256: b2dc2d6067ea62065439959ad41adc1be1d6ca8d4b522d038cd5ff5f7751788c

Contents?: true

Size: 830 Bytes

Versions: 133

Compression:

Stored size: 830 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.
  #
  #   hash = { a: 1, b: 2, c: 3, d: 4 }
  #   hash.extract!(:a, :b) # => {:a=>1, :b=>2}
  #   hash                  # => {:c=>3, :d=>4}
  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

133 entries across 127 versions & 18 rubygems

Version Path
activesupport-8.0.0.beta1 lib/active_support/core_ext/hash/slice.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/core_ext/hash/slice.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/core_ext/hash/slice.rb
omg-activesupport-8.0.0.alpha7 lib/active_support/core_ext/hash/slice.rb
omg-activesupport-8.0.0.alpha4 lib/active_support/core_ext/hash/slice.rb
omg-activesupport-8.0.0.alpha3 lib/active_support/core_ext/hash/slice.rb
omg-activesupport-8.0.0.alpha2 lib/active_support/core_ext/hash/slice.rb
omg-activesupport-8.0.0.alpha1 lib/active_support/core_ext/hash/slice.rb
activesupport-7.1.4 lib/active_support/core_ext/hash/slice.rb
activesupport-7.2.1 lib/active_support/core_ext/hash/slice.rb
activesupport-7.2.0 lib/active_support/core_ext/hash/slice.rb
activesupport-7.2.0.rc1 lib/active_support/core_ext/hash/slice.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/hash/slice.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/core_ext/hash/slice.rb
activesupport-7.2.0.beta3 lib/active_support/core_ext/hash/slice.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/hash/slice.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/hash/slice.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/hash/slice.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/hash/slice.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/hash/slice.rb