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-6.1.4.1 lib/active_support/core_ext/hash/slice.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4/lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.4 lib/active_support/core_ext/hash/slice.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.3.1/lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.3.2 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.3.1 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.3 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.2.1 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.2 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.1 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.0 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.0.rc2 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.0.rc1 lib/active_support/core_ext/hash/slice.rb