arrow_backward Back to blog

Using your dear friend, Enumerable

I recently had to look over some code that was returning inconsistent results. I was able to fix the code with a few minor changes, but it was still very hard to follow. Take a look:

So we use Hash#each to run through the hash, run our pass_or_fail method, and then run the setter method to save the result. This should really be refactored further into its own method, but I want to focus more on the Enumerable#inject method for this article.

I refactored the code below to move that ugly for loop into its own method, and used inject instead, which seems much cleaner to me. By moving the code to its own method, writing tests for the logic flow becomes easier. It also makes it easier to track down these type of problems in the future.


New Project Request