Last active
December 5, 2015 11:48
-
-
Save p-b-west/f8601618c51c8e22ba4e to your computer and use it in GitHub Desktop.
ruby inject examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This example from RubyMonk Ruby Primer:Ascent 7.1 in the discussion on 'inject'. | |
def occurrences(str) | |
str.scan(/\w+/).inject(Hash.new(0)) do |build, word| | |
# Changing Hash.new(0) to {} breaks this | |
build[word.downcase] +=1 | |
build | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment