lib/io_streams/tabular/header.rb in iostreams-1.3.1 vs lib/io_streams/tabular/header.rb in iostreams-1.3.2
- old
+ new
@@ -127,21 +127,20 @@
end
# Perform cleansing on returned Hash keys during the narrowing process.
# For example, avoids issues with case etc.
def cleanse_hash(hash)
- h = {}
- hash.each_pair do |key, value|
- cleansed_key =
- if columns.include?(key)
- key
- else
- key = cleanse_column(key)
- key if columns.include?(key)
- end
- h[cleansed_key] = value if cleansed_key
+ unmatched = columns - hash.keys
+ unless unmatched.empty?
+ hash = hash.dup
+ unmatched.each { |name| hash[cleanse_column(name)] = hash.delete(name) }
end
- h
+ # Hash#slice as of Ruby 2.5
+ if hash.respond_to?(:slice)
+ hash.slice(*columns)
+ else
+ columns.each_with_object({}) { |column, new_hash| new_hash[column] = hash[column] }
+ end
end
def cleanse_column(name)
cleansed = name.to_s.strip.downcase
cleansed.gsub!(/\s+/, "_")