Михаил обнови решението на 22.10.2016 13:55 (преди около 8 години)
+module HashAndArrayMethods
+ def fetch_deep(path)
+ key, rest = path.split '.', 2
+ value = get_value key
+ return value if rest.nil? || value.nil?
+ value.fetch_deep rest
+ end
+end
+
+class Hash
+ prepend HashAndArrayMethods
+
+ def get_value(key)
+ self[key] || self[key.to_sym]
+ end
+
+ def reshape(shape)
+ result = {}
+ shape.each do |k, v|
+ result[k] = v.is_a?(Hash) ? reshape(v) : fetch_deep(v)
+ end
+ result
+ end
+end
+
+class Array
+ prepend HashAndArrayMethods
+
+ def get_value(key)
+ self[Integer(key)]
+ end
+
+ def reshape(shape)
+ result = []
+ self.each do |v|
+ result.push v.reshape shape
+ end
+ result
+ end
+end