I was curious how I may refactor this code:
array.collect{|x| x.some_method}.inject(:+) || 0
I have it about ten times in my code with different methods so I thought I should refactor, but how?
I tried at least ten varieties of blocks, procs and lambdas and at this point I’m not even sure that it can be refactored.
Refactor how/to what? What’s the goal? Do you want/need to go beyond:
If the only thing that changes is the method being called, pass the method symbol in to a method and send it to each object during collection.
What are you trying to communicate with the code in question? Looks like some sort of summation; perhaps a simple
summethod with the “property” name as a parameter?