Fun with ruby `each_with_object` AKA it's Ruby there has to be a better way
November 20, 2018•261 words
I was solving ruby exercism "word count", and I stumbled into a common pattern:
def word_count
word_counter = {}
words.each do |word|
word_count = words.count word
word_counter[word] = word_count
end
word_counter
end
That could be summarized as:
Have an array that you have to do something about it
Create a new hash
Do something with enumerator
Return the new hash
Obviously the code is clear in it's intent, but it's too verbose and it's bugs me the to end the method with the ...
Read post