Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/fog_tracker/util/hash_symbolize_keys.rb

Overview

Turns all String keys in hash into Symbols – but ONLY IF there’s not already an existing symbol matching the String key

Instance Method Summary collapse

Instance Method Details

#symbolize_keys(recurse = true) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fog_tracker/util/hash_symbolize_keys.rb', line 4

def symbolize_keys(recurse = true)
  keys_do_delete = Array.new
  each do |k,v|
    if (k.instance_of? String) and (not has_key? k.to_sym)
      keys_do_delete << k
    end
    v.symbolize_keys(true) if (recurse && v.instance_of?(Hash))
  end
  keys_do_delete.each do |k|
    store(k.to_sym, fetch(k))
    delete k
  end
  self
end