Виктор обнови решението на 22.10.2016 02:42 (преди около 8 години)
+class Hash
+ def fetch_deep(path)
+ fetch_deep_and_parse(path) { |s| s.to_sym }
+ end
+
+ def reshape(shape)
+ shape.each_with_object({}) do |(k, v), hash|
+ hash[k] = if v.is_a? String
+ self.fetch_deep(v)
+ else
+ reshape(v)
+ end
+ end
+ end
+end
+
+class Array
+ def fetch_deep(path)
+ fetch_deep_and_parse(path) { |s| s.to_i }
+ end
+
+ def reshape(shape)
+ clone.map { |hash| hash.reshape(shape) }
+ end
+end
+
+def fetch_deep_and_parse(path)
+ head = path.partition('.').first
+ tail = path.partition('.').last
+ if tail.empty?
+ self[yield(head)] || self[head]
+ else
+ self[yield(head)]&.fetch_deep(tail) || self[head]&.fetch_deep(tail)
+ end
+end
ops