Skip to content

Instantly share code, notes, and snippets.

@kbaribeau
Created August 24, 2012 16:48
Show Gist options
  • Select an option

  • Save kbaribeau/3452778 to your computer and use it in GitHub Desktop.

Select an option

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?
@richievos
Copy link
Copy Markdown

Forgot a :c => 0 in there

# easy to see we're doing x = y
hash = {}
hash[:a] = [1, 2, 3, 4].map { |x| x + 1 }.inject(&:+)
hash[:b] = "asdf123"
hash[:c] = [2, 9, 8].map { |x| x * 2 }.inject(&:+)

# have to scan around to see what's going on
hash = { :a => 0, :c => 0 }
[1, 2, 3, 4].each { |x| hash[:a] += (x + 1) }
hash[:b] = "asdf123"
[2, 9, 8].each { |x| hash[:c] += x * 2 }.inject(&:+)

@kbaribeau
Copy link
Copy Markdown
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