Created
March 29, 2018 22:11
-
-
Save Ch4s3/baaff787b70ccb1fba636e73adf71ebc to your computer and use it in GitHub Desktop.
find and sum the count of occurances of a query_string in an array of strings
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
def find_suffix(collection, query_string) | |
# Inject 0 so that when evaluating the block, you have | |
# and object that responds to `+` | |
# if the array element is equal to the query_string | |
# add to the sum and return the sum to be used in | |
# the next step | |
collection.inject(0) { |sum, str| sum += 1 if str == query_string; sum } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment