Skip to content

Instantly share code, notes, and snippets.

@kbaribeau
Created August 24, 2012 16:48
Show Gist options
  • Save kbaribeau/3452778 to your computer and use it in GitHub Desktop.
Save kbaribeau/3452778 to your computer and use it in GitHub Desktop.
Ruby style poll
#Do you prefer this?
merchant["thing"] = important_stuff.map { |important_thing| important_thing["units"] }.inject(&:+)
#Or this?
important_stuff.each { |important_thing| merchant["thing"] += important_thing["units"] }
#Why? Is it completely objective? Or are ruby's idioms/social mores/conventions the deciding factor?
@kbaribeau
Copy link
Author

I still like the .sum solution, but @fredericksgary's point is what makes me like #1 a little more than just an inject.

Is there something that makes a pure inject clearer than solution #1? Objectively, it's one loop vs two. You could argue that one loop is executing fewer instructions, but I think two simple loops is clearer than one (slightly) more complex one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment