Sha256: 01a9590edd39d3005b3ed0fedd041802ee4bdbddd9a0951ed238563c182be835
Contents?: true
Size: 1.77 KB
Versions: 14
Compression:
Stored size: 1.77 KB
Contents
# frozen_string_literal: true # :nocov: # # Monkey patches for the ruby Hash # class Hash unless {}.respond_to?(:slice) # # Returns only the matching keys in a new hash # # @param [Array<String>, Array<Symbol>] keys the keys to match # # @return [Hash] # def slice(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if key?(k) } end end unless {}.respond_to?(:stringify_keys) # # Converts all keys to string # # # @return [Hash<String>] # def stringify_keys transform_keys(&:to_s) end end unless {}.respond_to?(:transform_keys) # # Transforms all keys by yielding to the caller # # # @return [Hash] # def transform_keys result = {} each_key do |key| result[yield(key)] = self[key] end result end end unless {}.respond_to?(:slice!) # # Removes all keys not provided from the current hash and returns it # # @param [Array<String>, Array<Symbol>] keys the keys to match # # @return [Hash] # def slice!(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) omit = slice(*self.keys - keys) hash = slice(*keys) hash.default = default hash.default_proc = default_proc if default_proc replace(hash) omit end end end # # Monkey patches for the ruby Array # class Array unless [].respond_to?(:extract_options!) # # Extract the last argument if it is a hash # # # @return [Hash] # def extract_options! if last.is_a?(Hash) && last.instance_of?(Hash) pop else {} end end end end
Version data entries
14 entries across 14 versions & 1 rubygems