Христо обнови решението на 24.10.2016 15:58 (преди около 8 години)
+class Hash
+ def fetch_deep(path)
+ fetch_deep_helper path, self
+ end
+
+ def reshape(shape)
+ new_hash = {}
+ shape.each do |k, v|
+ new_hash[k] = v.is_a?(String) ? fetch_deep(v) : reshape(shape[k])
+ end
+ replace new_hash
+ new_hash
+ end
+
+ def fetch_deep_helper(path, hash)
+ return hash if path.nil? || hash.nil?
+ path = path.split '.', 2
+ first = path[0]
+ path = path[1]
+ hash = get_new_hash first, hash
+ fetch_deep_helper path, hash
+ end
+
+ def get_new_hash(path, hash)
+ if hash.is_a? Array
+ num = Integer path rescue nil
+ num.nil? ? nil : hash[num]
+ elsif !hash[path].nil?
+ hash[path]
+ else
+ hash[path.to_sym]
+ end
+ end
+end
+
+class Array
+ def reshape(shape)
+ each { |i| i.reshape(shape) }
+ end
+end