Module: Eventoverse::Config

Defined in:
lib/eventoverse/config.rb

Class Method Summary collapse

Class Method Details

.get_in(hash, path) ⇒ Object

Returns value from hash based on the path

Examples:

get_in({:a => {:b => {:c => 2}}}, [:a, :b, :c]) # => 2
get_in({:a => {:b => {:c => 2}}}, [:a, :e, :c]) # => nil
get_in({:a => {:b => {:c => 2}}}, [:a, :b])     # => {:c=>2}

Parameters:

  • hash (Hash)
    • hash to retrieve values from

  • path (Array)
    • non-empty array representing recursive path



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/eventoverse/config.rb', line 16

def self.get_in(hash, path)
  raise "Path should be an array!" unless path.is_a?(Array)
  raise "Path should not be empty!" if path.empty?

  res = hash.fetch(path.shift, nil)
  if path.empty? || res.nil?
    res
  else
    get_in(res, path)
  end
end