Sha256: 485e04f2831bd960a665893f0db1817b2ea870f516b9ab05657ed4f2517625f8

Contents?: true

Size: 659 Bytes

Versions: 3

Compression:

Stored size: 659 Bytes

Contents

module Footing
  module Hash

    # Rekeys the Hash by invoking a method on the existing keys
    # and uses the return value as the new key.
    #
    # NOTE: Creates and return a new Hash.
    #
    # Example:
    #   h = { [1] => "short", [1,2] => "medium", [1,2,3] => "long" }
    #   h.rekey(:length) # => { 1 => "short", 2 => "medium", 3 => "long" }
    #
    # @param [Symbol] name The method name to invoke on the existing keys.
    # @return [Hash] A new Hash that has been re-keyed.
    def rekey(method_name)
      inject({}) do |new_hash, (key, value)|
        new_hash[key.send(method_name)] = value
        new_hash
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
footing-0.0.6 lib/extensions/hash.rb
footing-0.0.5 lib/extensions/hash.rb
footing-0.0.4 lib/extensions/hash.rb