Convert Hash keys to symbols
Extend Hash
class Hash
def keys_to_symbols!
self.keys.inject({}) { |i,k|
i.merge!({k.to_sym => self.values_at(k).first}) }
end
end
Use like this:
h = {"name" => "Oto", "twitter" => "otobrglez"}
And this...
puts h.keys_to_symbols!
... will produce this:
{:name => "Oto", :twitter => "otobrglez"}
Written by Oto Brglez
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#