Skip to content

Instantly share code, notes, and snippets.

@Ch4s3
Created March 29, 2018 22:11
Show Gist options
  • Save Ch4s3/baaff787b70ccb1fba636e73adf71ebc to your computer and use it in GitHub Desktop.
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
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