Skip to content

Instantly share code, notes, and snippets.

@aheadley
Created October 10, 2012 20:39
Show Gist options
  • Save aheadley/3868276 to your computer and use it in GitHub Desktop.
Save aheadley/3868276 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class NodeTest
def initialize(parent, name)
@parent = parent
@name = name
end
def add_node(name)
self.class.new(self, name)
end
def name()
@name
end
def parent()
@parent
end
def walk()
yield @parent.walk if @parent
yield self
end
def path()
self.walk {|node| puts node.name}
end
end
root = node = NodeTest.new(nil, 'root')
node_names = ['a', 'b', 'c']
for nn in node_names do
node = node.add_node(nn)
end
puts node.path
$ ruby test.rb
test.rb:23:in `walk': no block given (yield) (LocalJumpError)
from test.rb:22:in `walk'
from test.rb:22:in `walk'
from test.rb:22:in `walk'
from test.rb:27:in `path'
from test.rb:37:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment