Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb,
lib/openstudio-standards/utilities/hash.rb

Overview

Add a “dig” method to Hash to check if deeply nested elements exist From: stackoverflow.com/questions/1820451/ruby-style-how-to-check-whether-a-nested-hash-element-exists

Instance Method Summary collapse

Instance Method Details

#dig(*path) ⇒ Object


4
5
6
7
8
# File 'lib/openstudio-standards/utilities/hash.rb', line 4

def dig(*path)
  path.inject(self) do |location, key|
    location.respond_to?(:keys) ? location[key] : nil
  end
end

#sort_by_key(recursive = false, &block) ⇒ Object


9
10
11
12
13
14
15
16
17
# File 'lib/openstudio-standards/standards/necb/necb_2011/data/standards_data.rb', line 9

def sort_by_key(recursive = false, &block)
  self.keys.sort(&block).reduce({}) do |seed, key|
    seed[key] = self[key]
    if recursive && seed[key].is_a?(Hash)
      seed[key] = seed[key].sort_by_key(true, &block)
    end
    seed
  end
end