Христина обнови решението на 18.10.2016 22:01 (преди над 8 години)
+class Hash
+ def fetch_deeper(curr, result)
+ result = if result.is_a?(Array)
+ result.fetch(curr.to_i)
+ else
+ result = result.fetch(curr.to_sym) { result.fetch(curr, nil) }
+ end
+ end
+
+ def fetch_deep(path)
+ result = self
+ path_array = path.split('.')
+ until path_array.empty?
+ current_key = path_array.shift
+ result = fetch_deeper(current_key, result)
+ end
+ result
+ end
+
+ def reshape(shape_hash)
+ shape_hash.update(shape_hash) { |_, value| fetch_deep(value) }
+ end
+end
+
+class Array
+ def reshape(shape_hash)
+ map! { |element| element.reshape(shape_hash) }
+ end
+end