Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/hash_extension.rb
Instance Method Summary collapse
- #pair_at_chain(chain) ⇒ Object
- #recurse(include_root = false, depth = [], &block) ⇒ Object
- #value_at_chain(chain) ⇒ Object
Instance Method Details
#pair_at_chain(chain) ⇒ Object
18 19 20 21 22 |
# File 'lib/hash_extension.rb', line 18 def pair_at_chain(chain) chain = chain.dup chain.pop return self.value_at_chain(chain) end |
#recurse(include_root = false, depth = [], &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hash_extension.rb', line 24 def recurse(include_root=false, depth=[], &block) self.each_pair { |k,v| if v.is_a? Hash if include_root block.call(depth + [k], v) end depth.push k v.recurse(include_root, depth, &block) else block.call(depth + [k], v) #return "#{depth + [k]}: #{v.inspect}" end } depth.pop end |
#value_at_chain(chain) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/hash_extension.rb', line 2 def value_at_chain(chain) current = self chain.each do |key| if current.is_a? Hash and current.has_key? key current = current[key] if current.is_a? Array current = current.last end else current = nil break end end return current end |