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.7.3 lib/active_support/core_ext/hash/slice.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.7.2 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.4.2 lib/active_support/core_ext/hash/slice.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.4.1 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.7.1 lib/active_support/core_ext/hash/slice.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.4 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.7 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.3.1 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.6.1 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.6 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.3 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.2.4 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.5.1 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.5 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.2.3 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.4.7 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.2.2 lib/active_support/core_ext/hash/slice.rb