Commits

Steven! Ragnarök  committed 125f769

A simple tree in Ruby.

  • Participants
  • Parent commits 5740f00

Comments (0)

Files changed (1)

+class Tree
+  attr_accessor :children, :node_name
+
+  def initialize hash
+    @node_name = hash.keys.first
+    @children = hash[@node_name].map do |k,v|
+      Tree.new k => v
+    end
+  end
+
+  def visit_all &block
+    visit &block
+    children.each { |c| c.visit &block }
+  end
+
+  def visit
+    yield self
+  end
+end