Sha256: b2dc2d6067ea62065439959ad41adc1be1d6ca8d4b522d038cd5ff5f7751788c

Contents?: true

Size: 830 Bytes

Versions: 134

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

134 entries across 128 versions & 19 rubygems

Version Path
activesupport-7.0.2.2 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.4.6 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.4.5 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.2.1 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.2 lib/active_support/core_ext/hash/slice.rb
ric-0.14.2 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/core_ext/hash/slice.rb
ric-0.14.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/core_ext/hash/slice.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/activesupport-6.1.4.4/lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.1 lib/active_support/core_ext/hash/slice.rb
ric-0.14.0 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.0 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.4.4 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.0.rc3 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.4.3 lib/active_support/core_ext/hash/slice.rb
activesupport-6.1.4.2 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.0.rc2 lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.0.rc1 lib/active_support/core_ext/hash/slice.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/core_ext/hash/slice.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/core_ext/hash/slice.rb
activesupport-7.0.0.alpha2 lib/active_support/core_ext/hash/slice.rb