Костадин обнови решението на 21.10.2016 13:12 (преди около 8 години)
+def process_path_containing_array(hash_object, keys)
+ counter = -1
+ build_hash = hash_object[keys[0].to_sym].map do |element|
+ [(counter += 1).to_s.to_sym, element]
+ end.to_h
+ build_hash.fetch_deep(keys[1..-1].join('.'))
+end
+
+def process_path(keys)
+ if self.key? keys[0].to_sym
+ if self[keys[0].to_sym].is_a?(Hash)
+ self[keys[0].to_sym].fetch_deep(keys[1..-1].join('.'))
+ else
+ process_path_containing_array self, keys
+ end
+ end
+end
+
+class Hash
+ def fetch_deep(path)
+ if path.include? '.'
+ keys = path.split('.')
+ process_path keys
+ elsif self.key? path.to_sym
+ self[path.to_sym]
+ else
+ nil
+ end
+ end
+
+ def reshape(shape)
+ result = {}
+ shape.keys.each do |key|
+ result[key] = self.fetch_deep shape[key]
+ end
+ result
+ end
+end
+
+class Array
+ def reshape(shape)
+ result = []
+ self.each do |element|
+ target = {}
+ shape.keys.each { |key| target[key] = element.fetch_deep shape[key] }
+ result << target
+ end
+ result
+ end
+end