From: "marcandre (Marc-Andre Lafortune)" Date: 2013-01-14T13:59:15+09:00 Subject: [ruby-core:51418] [ruby-trunk - Bug #7248] Shouldn't Enumerator::Lazy.new be private? Issue #7248 has been updated by marcandre (Marc-Andre Lafortune). Priority changed from Normal to High Thanks for the answers. So, the public API of Lazy.new has the following issues: * not documented * should require a block but doesn't * accepts a method name & arguments which aren't really usable * has a misleading "inspect" Moreover, the only way for the user to create a lazy enumerator with a size is to subclass Lazy. Here's what I propose as the official Lazy.new documentation and API: Lazy.new(obj, size=nil) { |yielder, *values| ... } Creates a new Lazy enumerator. When the enumerator is actually enumerated (e.g. by calling #force), +obj+ will be enumerated and each value passed to the given block. The block can yield values back using +yielder+. For example, to create a method +filter_map+ in both lazy and non-lazy fashions: module Enumerable def filter_map(&block) map(&block).compact end end class Enumerator::Lazy def filter_map Lazy.new(self) do |yielder, *values| result = yield *values yielder << result if result end end end (1..Float::INFINITY).lazy.filter_map{|i| i*i if i.even?}.first(5) # => [4, 16, 36, 64, 100] Does this seem acceptable to you? We should also change the result of the 'inspect' method for these user created lazy enumerators to remove the 'each', i.e: (1..Float::INFINITY).lazy.filter_map{|i| i*i if i.even?}.inspect # => #> If we want to provide an easy way to provide more info in the inspect, we could add extra parameters, but they shouldn't be used when iterating, only for inspection... > BTW, I have a question. Document of to_enum says "see Enumerator#size=" but there is no such method. Is it a typo? Typo fixed, thanks. ---------------------------------------- Bug #7248: Shouldn't Enumerator::Lazy.new be private? https://bugs.ruby-lang.org/issues/7248#change-35397 Author: marcandre (Marc-Andre Lafortune) Status: Assigned Priority: High Assignee: marcandre (Marc-Andre Lafortune) Category: core Target version: 2.0.0 ruby -v: ruby 2.0.0dev (2012-10-29 trunk 37380) [x86_64-darwin10.8.0] Is there a reason why Enumerator::Lazy.new is not private? Lazy enumerators should be created with `Enumerable#lazy`. Moreover, there is no doc, and it can give unexpected results too. -- http://bugs.ruby-lang.org/