Skip to content

Instantly share code, notes, and snippets.

@ahoward ahoward/gist:157787
Created Jul 29, 2009

Embed
What would you like to do?
# 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
You can’t perform that action at this time.