Enumerator of what?

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]

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 => 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

On 7/18/09, Intransition [email protected] wrote:

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]


Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

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

[Antoine de Saint-Exupéry]

but this patch could do it if you really need it

Is this functionality…useful enough to merit being submitted to core?

On 7/18/09, Roger P. [email protected] wrote:

but this patch could do it if you really need it

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


Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

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

[Antoine de Saint-Exupéry]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs