From: w_a_x_man Date: 2010-12-28T03:10:16+09:00 Subject: Re: converting traversal fn into Enumeration On Dec 26, 8:23 am, Michael Fellinger wrote: > On Sun, Dec 26, 2010 at 7:27 PM, Brian Candler wrote: > > 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 > > The usual way to return enumerators in 1.9 is this: > > def traverse >   return to_enum(__method__) unless block_given? >   loop{ yield(rand(10)) } > end > > traverse.find{|e| e > 1 } > # => 7 Works in 1.8.7 also.