From: Brian Candler Date: 2010-12-26T19:27:15+09:00 Subject: Re: converting traversal fn into Enumeration It's easier than you think. Just define a method called 'each' which enumerates the objects, and mix-in Enumerable into your class. class TNode include Enumerable alias :each :traverse end > For somewhat didactic reasons, I'd prefer a method TNode#traversal(), > that returns an Enumerable You cannot return an Enumerable, because it is a Module, not a Class. Hence you can't create instances of Enumerable. , so I could do something like: > ----- > def find_node(value) > traversal.find {|n| n.value == value} > end > ----- What I explained above lets you do it directly on the node: rootnode.find { |n| n.values == value } If you don't want to do this on the node class itself, you can instead return some other object which has a #each method: class TNode class Traversal include Enumerable def initialize(root) @root = root end def each ... define it here end end def traversal Traversal.new(self) end end Or there is class Enumerator in the standard library which you can use (in 1.8 it's Enumerable::Enumerator). It just gives a wrapper object which maps a call to :each to some other method in the underlying object. require 'enumerator' class TNode def traversal Enumerable::Enumerator.new(self, :traverse) end end -- Posted via http://www.ruby-forum.com/.