Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/hash/symbolize_keys.rb

Instance Method Summary collapse

Instance Method Details

#symbolize_keys(&select) ⇒ Object



2
3
4
# File 'lib/core_ext/hash/symbolize_keys.rb', line 2

def symbolize_keys(&select)
  dup.symbolize_keys!(&select)
end

#symbolize_keys!(&select) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/core_ext/hash/symbolize_keys.rb', line 6

def symbolize_keys!(&select)
  if select
    keys.each do |key|
      if select[key]
        new_key = (key.to_sym rescue key.to_s.to_sym)
        self[new_key] = delete(key)
      end
    end
  else
    keys.each do |key|
      new_key = (key.to_sym rescue key.to_s.to_sym)
      self[new_key] = delete(key)
    end
  end
  self
end