Sha256: 56fe8e83db11dbaea9bea6ab79810a6babe0de27a55d42b6892aaf4459377d12

Contents?: true

Size: 1.85 KB

Versions: 15

Compression:

Stored size: 1.85 KB

Contents

module Ardm; module Ext
  module Hash
    # Creates a hash with *only* the specified key/value pairs from +hash+.
    #
    # @param [Hash] hash The hash from which to pick the key/value pairs.
    # @param [Array] *keys The hash keys to include.
    #
    # @return [Hash] A new hash with only the selected keys.
    #
    # @example
    #   hash = { :one => 1, :two => 2, :three => 3 }
    #   Ext::Hash.only(hash, :one, :two) # => { :one => 1, :two => 2 }
    #
    # @api semipublic
    def self.only(hash, *keys)
      h = {}
      keys.each {|k| h[k] = hash[k] if hash.has_key?(k) }
      h
    end

    # Returns a hash that includes everything but the given +keys+.
    #
    # @param [Hash] hash The hash from which to pick the key/value pairs.
    # @param [Array] *keys The hash keys to exclude.
    #
    # @return [Hash] A new hash without the specified keys.
    #
    # @example
    #   hash = { :one => 1, :two => 2, :three => 3 }
    #   Ext::Hash.except(hash, :one, :two) # => { :three => 3 }
    #
    # @api semipublic
    def self.except(hash, *keys)
      self.except!(hash.dup, *keys)
    end

    # Removes the specified +keys+ from the given +hash+.
    #
    # @param [Hash] hash The hash to modify.
    # @param [Array] *keys The hash keys to exclude.
    #
    # @return [Hash] +hash+
    #
    # @example
    #   hash = { :one => 1, :two => 2, :three => 3 }
    #   Ext::Hash.except!(hash, :one, :two)
    #   hash # => { :three => 3 }
    #
    # @api semipublic
    def self.except!(hash, *keys)
      keys.each { |key| hash.delete(key) }
      hash
    end

    # Converts the specified +hash+ to a {Mash}.
    #
    # @param [Hash] hash The hash to convert.
    # @return [Mash] The {Mash} for the specified +hash+.
    #
    # @api semipublic
    def self.to_mash(hash)
      h = Mash.new(hash)
      h.default = hash.default
      h
    end
  end
end; end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ardm-0.4.0.ar427 lib/ardm/support/ext/hash.rb
ardm-0.4.0 lib/ardm/support/ext/hash.rb
ardm-0.3.2 lib/ardm/support/ext/hash.rb
ardm-0.3.1 lib/ardm/support/ext/hash.rb
ardm-0.3.0 lib/ardm/support/ext/hash.rb
ardm-0.2.7 lib/ardm/support/ext/hash.rb
ardm-0.2.6 lib/ardm/support/ext/hash.rb
ardm-0.2.5 lib/ardm/support/ext/hash.rb
ardm-0.2.4 lib/ardm/support/ext/hash.rb
ardm-0.2.3 lib/ardm/support/ext/hash.rb
ardm-0.2.2 lib/ardm/support/ext/hash.rb
ardm-0.2.1 lib/ardm/support/ext/hash.rb
ardm-0.2.0 lib/ardm/support/ext/hash.rb
ardm-0.1.0 lib/ardm/support/ext/hash.rb
ardm-0.0.1 lib/ardm/support/ext/hash.rb