Георги обнови решението на 23.10.2016 22:35 (преди около 8 години)
+class Hash
+ def fetch_deep(path)
+ key, rest = path.to_s.partition('.').values_at(0, -1)
+ value = (self.key? key) ? self[key] : self[key.to_sym]
+ if value.nil? || rest.empty?
+ value
+ else
+ value.fetch_deep(rest)
+ end
+ end
+
+ def reshape(shape)
+ result = shape.clone
+ result.each_key do |key|
+ reshape(result[key]) if result[key].is_a? Hash
+ reshape(result[key]) if result[key].is_a? Array
+ result[key] = fetch_deep(result[key])
+ result
+ end
+ end
+end
+
+class Array
+ def fetch_deep(path)
+ index, rest = path.partition('.').values_at(0, -1)
+ value = self[index.to_i]
+ if value.nil? || rest.empty?
+ value
+ else
+ value.fetch_deep(rest)
+ end
+ end
+
+ def reshape(shape)
+ map { |element| element.reshape(shape) }
+ end
+end