Enumerator of what?

I

Intransition

Any way to ask an Enumerator what the underlying object it is
enumerating for is? Eg.

e = Enumerator.new([1,2,3], :each)
e.some_method #=> [1,2,3]
 
R

Robert Dober

Not as far as I know,

but this patch could do it if you really need it

--- enumerator.c 2009-04-19 15:33:31.000000000 +0200
+++ enumerator-with-obj-access.c 2009-07-18 18:02:33.000000000 +0200
@@ -537,6 +537,12 @@
return v;
}

+static VALUE
+enumerator_get_object( VALUE obj )
+{
+ return enumerator_ptr( obj )->obj;
+}
+
/*
* call-seq:
* e.rewind =3D> e
@@ -788,6 +794,7 @@
rb_define_method(rb_cEnumerator, "initialize", enumerator_initialize, =
-1);
rb_define_method(rb_cEnumerator, "initialize_copy",
enumerator_init_copy, 1);
rb_define_method(rb_cEnumerator, "each", enumerator_each, 0);
+ rb_define_method(rb_cEnumerator, "get_object", enumerator_get_object, =
0);
rb_define_method(rb_cEnumerator, "each_with_index",
enumerator_with_index, 0);
rb_define_method(rb_cEnumerator, "each_with_object",
enumerator_with_object, 1);
rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, =
0);


please note that
2.times.get_object will give you 2 and not [0, 1] which 2.times.to_a
would give you.

Cheers
Robert

Any way to ask an Enumerator what the underlying object it is
enumerating for is? Eg.

e =3D Enumerator.new([1,2,3], :each)
e.some_method #=3D> [1,2,3]


--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]
 
R

Robert Dober

Is this functionality...useful enough to merit being submitted to core?
I do not think so, I was in a strange mood and as there is no other way...
I suspect that enumerator hides its obj by designed.

But of course this patch is in the public domain now.

Cheers
Robert

--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top