Петър обнови решението на 23.10.2016 21:56 (преди около 8 години)
+class Hash
+ def get_value(possible_key)
+ return self[possible_key] if self[possible_key]
+
+ symbolic_key = possible_key.to_sym
+ return self[symbolic_key] if self[symbolic_key]
+
+ integer_key = possible_key.to_i
+ return self[integer_key] if self[integer_key]
+ end
+
+ def fetch_deep(hash_path)
+ keys = hash_path.split '.'
+
+ keys.reduce(self) do |value, key|
+ return nil if value.nil?
+ (value.is_a? Array) ? value[key.to_i] : value.get_value(key)
+ end
+ end
+
+ def reshape(shape)
+ new_shape = shape.map do |key, value|
+ (value.is_a? Hash) ? [key, reshape(value)] : [key, fetch_deep(value)]
+ end.to_h
+
+ replace new_shape
+
+ new_shape
+ end
+end
+
+class Array
+ def reshape(shape)
+ each { |element| element.reshape(shape) }
+ end
+end