Skip to content

Instantly share code, notes, and snippets.

@jcalvert
Created April 15, 2011 17:01
Show Gist options
  • Save jcalvert/922038 to your computer and use it in GitHub Desktop.
Save jcalvert/922038 to your computer and use it in GitHub Desktop.
Is there a better way?
class Array
def to_hash(&block)
Hash[*self.collect { |v|
block.call(v).flatten
}.flatten]
end
end
# so that ["foobar", "barfoo"].to_hash{|i| [i.upcase, i.downcase] }
# yields {"FOOBAR"=>"foobar", "BARFOO"=>"barfoo"}
# to create a hash with keys and values coming from invocations of a method on an array element
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment