Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created July 29, 2009 00:50
Show Gist options
  • Save ahoward/157787 to your computer and use it in GitHub Desktop.
Save ahoward/157787 to your computer and use it in GitHub Desktop.
# 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