Module: Structverse

Defined in:
lib/structverse.rb

Constant Summary collapse

VERSION =
'1.0'

Class Method Summary collapse

Class Method Details

.walk(struct, &block) ⇒ Object

Walks an array or hash structure.

Structverse.walk(hsh) do |struct|
  puts struct.class
end


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/structverse.rb', line 11

def self.walk(struct, &block)
  if struct.is_a?(Hash) or struct.is_a?(Array)
    yield struct
    
    if struct.is_a?(Hash)
      struct.values.each do |v|
        walk v, &block
      end
      
    elsif struct.is_a?(Array)
      struct.each do |v|
        walk v, &block
      end
    end
  end
end