Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/ppy/nested_access.rb

Instance Method Summary collapse

Instance Method Details

#nested_access(path, seperator = '.') ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ppy/nested_access.rb', line 4

def nested_access(path, seperator = '.')
  ret = self
  path.to_s.split(seperator).each do |p|
    if p.to_i.to_s == p
      ret = ret[p.to_i]
    elsif !ret[p.to_s].nil?
      ret = ret[p.to_s]
    elsif !ret[p.to_sym].nil?
      ret = ret[p.to_sym]
    else
      ret = nil
    end
    break unless ret
  end

  ret
end