Created
October 10, 2012 20:39
-
-
Save aheadley/3868276 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c.b.a.root |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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