Created
July 29, 2009 00:50
-
-
Save ahoward/157787 to your computer and use it in GitHub Desktop.
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
# regarding : http://gist.github.com/157782 | |
# regarding : http://drawohara.com/post/151193800/ruby-symbol-to-hash | |
# | |
class Symbol | |
def to_h(&block) | |
Hash[to_sym, block && (block.call || eval(to_s, block))] | |
end | |
end | |
class Array | |
def to_h(&block) | |
flatten.inject({}){|h, v| h.update(v.to_h(&block))} | |
end | |
end | |
a = 4 | |
b = 2 | |
c = 42 | |
p [:a, :b, :c].to_h{} | |
__END__ | |
cfp:~ > ruby a.rb | |
{:a=>4, :b=>2, :c=>42} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment