From: Brian Candler Date: 2008-11-03T06:24:47+09:00 Subject: Re: ruby1.9: lazy versions of Enumerator#select and friends? Thomas Sawyer wrote: > Enumerator.new() wants an enumerable object as an argument --what is > 1.9 doing without it? 1.9 supports two completely different types of Enumerator objects. The first is the simple 1.8.6 one which just maps method :each to arbitrary method :foo on the upstream object. The second is the clever one. -------------------------------------------------------- Enumerator::new Enumerator.new(obj, method = :each, *args) Enumerator.new { |y| ... } From Ruby 1.9.1 ------------------------------------------------------------------------ Creates a new Enumerator object, which is to be used as an Enumerable object iterating in a given way. In the first form, a generated Enumerator iterates over the given object using the given method with the given arguments passed. Use of this form is discouraged. Use Kernel#enum_for(), alias to_enum, instead. e = Enumerator.new(ObjectSpace, :each_object) #-> ObjectSpace.enum_for(:each_object) e.select { |obj| obj.is_a?(Class) } #=> array of all classes In the second form, iteration is defined by the given block, in which a "yielder" object given as block parameter can be used to yield a value by calling the +yield+ method, alias +<<+. fib = Enumerator.new { |y| a = b = 1 loop { y << a a, b = b, a + b } } p fib.take(10) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] -- Posted via http://www.ruby-forum.com/.