Sha256: c1718c2ba39f78d6eaaca62dbdc5b967187dffe9608b57d9a33036852d451592

Contents?: true

Size: 812 Bytes

Versions: 131

Compression:

Stored size: 812 Bytes

Contents

# frozen_string_literal: true

class Hash
  # Returns a hash that includes everything except given keys.
  #   hash = { a: true, b: false, c: nil }
  #   hash.except(:c)     # => { a: true, b: false }
  #   hash.except(:a, :b) # => { c: nil }
  #   hash                # => { a: true, b: false, c: nil }
  #
  # This is useful for limiting a set of parameters to everything but a few known toggles:
  #   @person.update(params[:person].except(:admin))
  def except(*keys)
    slice(*self.keys - keys)
  end unless method_defined?(:except)

  # Removes the given keys from hash and returns it.
  #   hash = { a: true, b: false, c: nil }
  #   hash.except!(:c) # => { a: true, b: false }
  #   hash             # => { a: true, b: false }
  def except!(*keys)
    keys.each { |key| delete(key) }
    self
  end
end

Version data entries

131 entries across 125 versions & 16 rubygems

Version Path
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/hash/except.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/hash/except.rb
activesupport-7.2.0.beta2 lib/active_support/core_ext/hash/except.rb
activesupport-7.1.3.4 lib/active_support/core_ext/hash/except.rb
activesupport-7.0.8.4 lib/active_support/core_ext/hash/except.rb
activesupport-6.1.7.8 lib/active_support/core_ext/hash/except.rb
activesupport-7.2.0.beta1 lib/active_support/core_ext/hash/except.rb
activesupport-7.1.3.2 lib/active_support/core_ext/hash/except.rb
activesupport-7.1.3.1 lib/active_support/core_ext/hash/except.rb
activesupport-7.0.8.1 lib/active_support/core_ext/hash/except.rb
activesupport-6.1.7.7 lib/active_support/core_ext/hash/except.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/core_ext/hash/except.rb
activesupport-7.1.3 lib/active_support/core_ext/hash/except.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/core_ext/hash/except.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/core_ext/hash/except.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/core_ext/hash/except.rb
activesupport-7.1.2 lib/active_support/core_ext/hash/except.rb
activesupport-7.1.1 lib/active_support/core_ext/hash/except.rb
activesupport-7.1.0 lib/active_support/core_ext/hash/except.rb
activesupport-7.1.0.rc2 lib/active_support/core_ext/hash/except.rb