Method: Hash#symbolize_keys!
- Defined in:
- lib/core/facets/hash/symbolize_keys.rb
#symbolize_keys!(&select) ⇒ Object
Destructively convert all keys to symbols. This is the same as Hash#symbolize_keys, but modifies the receiver in place and returns it. With a select block, limits conversion to only selected keys.
foo = { 'name'=>'Gavin', 'wife'=>:Lisa }
foo.symbolize_keys! #=> { :name=>"Gavin", :wife=>:Lisa }
foo #=> { :name=>"Gavin", :wife=>:Lisa }
If the key does not respond to #to_sym, then #to_s will be used first.
For a more versatile method, see #rekey instead.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/core/facets/hash/symbolize_keys.rb', line 34 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 |