Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#make_indifferent!Object



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

def make_indifferent!
  keys_values = self.dup
  replace(Hash.new { |h,k| h[k.to_s] if Symbol === k })
  merge!(keys_values)
end

#nest!Object



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

def nest!
  new_params = Hash.new.make_indifferent!
  each_pair do |full_key, value| 
    this_param = new_params
    split_keys = full_key.to_s.split(/\]\[|\]|\[/)
    split_keys.each_index do |index| 
      break if split_keys.length == index + 1 
      this_param[split_keys[index]] ||= Hash.new.make_indifferent!
      this_param = this_param[split_keys[index]] 
    end 
    this_param[split_keys.last] = value 
  end 
  clear
  replace(new_params)
end