Sort of.  I'm still unclear how the actual node is propagated back up as 
the result of the find call due to the name == "right" block returning 
true.

Now how do I put this on a class which is an ActiveRecord (i.e. the find 
method is already taken)?

BTW Saw your talk at the LSRC.  I was very inspired to use Ruby for 
everything gluey.  :-)

James Gray wrote:
> 
> Does this give you some ideas?
> 
> #!/usr/bin/env ruby -wKU
> 
> class Tree
>    def initialize(name)
>      @name   = name
>      @leaves = Array.new
>    end
> 
>    attr_reader :name
> 
>    def <<(leaf_name)
>      @leaves << self.class.new(leaf_name)
>      self
>    end
> 
>    include Enumerable
> 
>    def each(&block)
>      block[self]
>      @leaves.each { |leaf| leaf.each(&block) }
>    end
> 
>    def to_s(indent = String.new)
>      str = "#{indent}#{@name}\n"
>      @leaves.each { |leaf| str += leaf.to_s(indent + "  ") }
>      str
>    end
> end
> 
> tree = Tree.new("top")
> tree << "left" << "right"
> 
> tree.find { |leaf| leaf.name == "right" } << "deep"
> 
> puts tree
> 
> __END__
> 
> James Edward Gray II

-- 
Posted via http://www.ruby-forum.com/.