I messed with this indentation. Originally it read as if the Y.each was completed in just a couple of lines due to indentation.

I spread it out a bit to try to make the blocks of logic more visible.