ruby-changes:27004
From: marcandre <ko1@a...>
Date: Tue, 5 Feb 2013 12:49:31 +0900 (JST)
Subject: [ruby-changes:27004] marcandRe: r39056 (trunk): * enumerator.c: Fix and improve rdoc for to_enum
marcandre 2013-02-05 12:49:22 +0900 (Tue, 05 Feb 2013) New Revision: 39056 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39056 Log: * enumerator.c: Fix and improve rdoc for to_enum Modified files: trunk/enumerator.c Index: enumerator.c =================================================================== --- enumerator.c (revision 39055) +++ enumerator.c (revision 39056) @@ -184,23 +184,18 @@ enumerator_ptr(VALUE obj) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L184 /* * call-seq: - * obj.to_enum(method = :each, *args) - * obj.enum_for(method = :each, *args) - * obj.to_enum(method = :each, *args) {|obj, *args| block} - * obj.enum_for(method = :each, *args){|obj, *args| block} - * - * Creates a new Enumerator which will enumerate by on calling +method+ on - * +obj+. - * - * +method+:: the method to call on +obj+ to generate the enumeration - * +args+:: arguments that will be passed in +method+ <i>in addition</i> - * to the item itself. Note that the number of args - * must not exceed the number expected by +method+ + * obj.to_enum(method = :each, *args) -> enum + * obj.enum_for(method = :each, *args) -> enum + * obj.to_enum(method = :each, *args) {|*args| block} -> enum + * obj.enum_for(method = :each, *args){|*args| block} -> enum * - * If a block is given, it will be used to calculate the size of - * the enumerator (see Enumerator#size). + * Creates a new Enumerator which will enumerate by calling +method+ on + * +obj+, passing +args+ if any. * - * === Example + * If a block is given, it will be used to calculate the size of + * the enumerator without the need to iterate it (see Enumerator#size). + * + * === Examples * * str = "xyz" * @@ -214,6 +209,33 @@ enumerator_ptr(VALUE obj) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L209 * a = [1, 2, 3] * some_method(a.to_enum) * + * It is typical to call to_enum when defining methods for + * a generic Enumerable, in case no block is passed. + * + * Here is such an example, with parameter passing and a sizing block: + * + * module Enumerable + * # a generic method to repeat the values of any enumerable + * def repeat(n) + * raise ArgumentError, "#{n} is negative!" if n < 0 + * unless block_given? + * return to_enum(__method__, n) do # __method__ is :repeat here + * sz = size # Call size and multiply by n... + * sz * n if sz # but return nil if size itself is nil + * end + * end + * each do |*val| + * n.times { yield *val } + * end + * end + * end + * + * %i[hello world].repeat(2) { |w| puts w } + * # => Prints 'hello', 'hello', 'world', 'world' + * enum = (1..14).repeat(3) + * # => returns an Enumerator when called without a block + * enum.first(4) # => [1, 1, 1, 2] + * enum.size # => 42 */ static VALUE obj_to_enum(int argc, VALUE *argv, VALUE obj) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/