Божидар обнови решението на 23.10.2016 14:53 (преди около 8 години)
+class Hash
+ def fetch_key_from_collection(collection, key)
+ return collection[Integer(key)] if collection.is_a?(Array)
+ [key, key.to_sym, key.to_r, key.to_i].each do |k|
+ return collection[k] if collection.key? k
+ end
+ nil
+ end
+
+ def fetch_deep(path)
+ keys = path.split('.')
+ keys.reduce(self) do |coll, key|
+ return nil if coll.nil?
+ fetch_key_from_collection(coll, key)
+ end
+ end
+
+ def reshape(shape)
+ return fetch_deep(shape) if shape.is_a?(String)
+ shape.map { |k, v| [k, reshape(v)] } .to_h
+ end
+end
+
+class Array
+ def reshape(shape)
+ map { |hash| hash.reshape(shape) }
+ end
+end
+