Sha256: a88b5c46d19f812b8cb257990e6bb2398e2ec46b65d1733028fd2e10b56390e6

Contents?: true

Size: 843 Bytes

Versions: 82

Compression:

Stored size: 843 Bytes

Contents

require 'set'

module ActiveSupport #:nodoc:
  module CoreExtensions #:nodoc:
    module Hash #:nodoc:
      # Slice a hash to include only the given keys. This is useful for
      # limiting an options hash to valid keys before passing to a method:
      #
      #   def search(criteria = {})
      #     assert_valid_keys(:mass, :velocity, :time)
      #   end
      #
      #   search(options.slice(:mass, :velocity, :time))
      module Slice
        # Returns a new hash with only the given keys.
        def slice(*keys)
          allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
          reject { |key,| !allowed.include?(key) }
        end

        # Replaces the hash with only the given keys.
        def slice!(*keys)
          replace(slice(*keys))
        end
      end
    end
  end
end

Version data entries

82 entries across 82 versions & 7 rubygems

Version Path
vibes-bj-1.2.2 spec/rails_root/vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
vibes-bj-1.2.1 spec/rails_root/vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
activesupport-2.0.1 lib/active_support/core_ext/hash/slice.rb
activesupport-2.0.0 lib/active_support/core_ext/hash/slice.rb
activesupport-2.0.4 lib/active_support/core_ext/hash/slice.rb
activesupport-2.0.5 lib/active_support/core_ext/hash/slice.rb
activesupport-2.0.2 lib/active_support/core_ext/hash/slice.rb
activesupport-2.1.0 lib/active_support/core_ext/hash/slice.rb
antfarm-0.3.0 rails/vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
antfarm-0.4.0 rails/vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.0.0 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.0.1 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.0.2 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.0.4 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.0.5 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.1.1 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.1.0 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.1.2 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.2.0 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.2.1 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb