Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/tryouts/mixins/hash.rb

Direct Known Subclasses

Tryouts::OrderedHash

Instance Method Summary collapse

Instance Method Details

#deepest_point(h = self, steps = 0) ⇒ Object

A depth-first look to find the deepest point in the Hash. The top level Hash is counted in the total so the final number is the depth of its children + 1. An example:

ahash = { :level1 => { :level2 => {} } }
ahash.deepest_point  # => 3


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tryouts/mixins/hash.rb', line 11

def deepest_point(h=self, steps=0)
  if h.is_a?(Hash)
    steps += 1
    h.each_pair do |n,possible_h|
      ret = deepest_point(possible_h, steps)
      steps = ret if steps < ret
    end
  else
    return 0
  end
  steps
end

#lastObject

Ruby 1.9 doesn’t have a Hash#last (but Tryouts::OrderedHash does). It’s used in Tryouts to return the most recently added instance of Tryouts to @@instances.

NOTE: This method is defined only when Hash.method_defined?(:last) returns false.



31
32
33
# File 'lib/tryouts/mixins/hash.rb', line 31

def last
  self[ self.keys.last ]
end