Skip to content

Instantly share code, notes, and snippets.

@bullfight
Forked from snuxoll/collect_keyvalue.rb
Created February 13, 2011 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bullfight/824721 to your computer and use it in GitHub Desktop.
Save bullfight/824721 to your computer and use it in GitHub Desktop.
# Module containing a collect_kv method to allow
# a new hash to be generated my collect instead
# of returning an array of modified keys or values
module CollectKeyValue
# Takes a block that returns a [key, value] pair
# and builds a new hash based on those pairs
def collect_kv
result = {}
each do |k,v|
new_k, new_v = yield k, v
result[new_k] = new_v
end
result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment