aboutsummaryrefslogtreecommitdiffstats
path: root/enumerator.c
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:10:20 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:10:20 +0000
commitacfd34a6a9e6462ed05f74e07f204b98d93126ff (patch)
tree07e3aab9366f774a858aa19bfd3b409261a584f5 /enumerator.c
parent610eeffa468f43c13fd374c53b5ed5cb5dd9c1b3 (diff)
downloadruby-acfd34a6a9e6462ed05f74e07f204b98d93126ff.tar.gz
* enumerator.c (obj_to_enum): Have #to_enum accept a block
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37498 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enumerator.c')
-rw-r--r--enumerator.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/enumerator.c b/enumerator.c
index 7695ea1a56..36e70fd084 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -186,6 +186,8 @@ enumerator_ptr(VALUE obj)
* 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+.
@@ -195,6 +197,9 @@ enumerator_ptr(VALUE obj)
* to the item itself. Note that the number of args
* must not exceed the number expected by +method+
*
+ * If a block is given, it will be used to calculate the size of
+ * the enumerator (see Enumerator#size=).
+ *
* === Example
*
* str = "xyz"
@@ -213,13 +218,17 @@ enumerator_ptr(VALUE obj)
static VALUE
obj_to_enum(int argc, VALUE *argv, VALUE obj)
{
- VALUE meth = sym_each;
+ VALUE enumerator, meth = sym_each;
if (argc > 0) {
--argc;
meth = *argv++;
}
- return rb_enumeratorize(obj, meth, argc, argv, 0);
+ enumerator = rb_enumeratorize(obj, meth, argc, argv, 0);
+ if (rb_block_given_p()) {
+ enumerator_ptr(enumerator)->size = rb_block_proc();
+ }
+ return enumerator;
}
static VALUE