Created
April 15, 2011 17:01
-
-
Save jcalvert/922038 to your computer and use it in GitHub Desktop.
Is there a better way?
This file contains 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
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