Петър обнови решението на 20.10.2016 08:20 (преди около 8 години)
+class Hash
+ def fetch_deep(path)
+ path.split('.').reduce(self) do |memo, key|
+ memo[key.to_i] || memo[key.to_sym] || memo[key.to_s] if memo
+ end
+ end
+end
+
+# class Hash
+# def fetch_deep(path)
+# cpy = self.clone
+# path.split('.').each do |t|
+# break unless cpy = cpy[t.to_i] || cpy[t.to_sym] || cpy[t.to_s]
+# end
+# return cpy
+# end
+# end
+
+class Hash
+ def reshape(shape)
+ sh = shape.clone
+ sh.each do |key, value|
+ if sh[key].is_a?(String)
+ sh[key] = self.fetch_deep(value)
+ else
+ self.each { sh[key] = self.reshape(sh[key]) }
+ end
+ end
+ end
+end
+
+class Array
+ def reshape(shape)
+ sh = shape.clone
+ self.map { |i| i.reshape(sh) }
+ end
+end