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
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.8.7/lib/active_support/core_ext/hash/slice.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/hash/slice.rb
activesupport-8.0.1 lib/active_support/core_ext/hash/slice.rb
activesupport-8.0.0.1 lib/active_support/core_ext/hash/slice.rb
activesupport-7.2.2.1 lib/active_support/core_ext/hash/slice.rb
activesupport-7.1.5.1 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.8.7 lib/active_support/core_ext/hash/slice.rb
activesupport-8.0.0 lib/active_support/core_ext/hash/slice.rb
activesupport-7.2.2 lib/active_support/core_ext/hash/slice.rb
activesupport-7.1.5 lib/active_support/core_ext/hash/slice.rb
activesupport-8.0.0.rc2 lib/active_support/core_ext/hash/slice.rb
activesupport-7.2.1.2 lib/active_support/core_ext/hash/slice.rb
activesupport-7.1.4.2 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.8.6 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.7.10 lib/active_support/core_ext/hash/slice.rb
activesupport-8.0.0.rc1 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.7.9 lib/active_support/core_ext/hash/slice.rb
activesupport-7.2.1.1 lib/active_support/core_ext/hash/slice.rb
activesupport-7.1.4.1 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.8.5 lib/active_support/core_ext/hash/slice.rb