Programifications

A mashup of technical quirks

Hash#recursive_symbolize_keys

| Comments

Surprisingly there isn’t a Hash#recursive_symbolize_keys method so here’s code to accomplish that:

1
2
3
4
5
6
7
class Hash
  def recursive_symbolize_keys
    symbolize_keys!
    values.select{|v| v.is_a? Hash}.each{|h| h.recursive_symbolize_keys}
    self
  end
end

Sometimes gems/plugins only take arguments in the form of symbols so this can method can be handy if you have some sort of nested config hash.

Credits to: http://grosser.it/2009/04/14/recursive-symbolize_keys/

Comments