Enumerator of what?

Discussion in 'Ruby' started by Intransition, Jul 18, 2009.

  1. Intransition

    Intransition Guest

    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]
    Intransition, Jul 18, 2009
    #1
    1. Advertising

  2. Intransition

    Robert Dober Guest

    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

    On 7/18/09, Intransition <> wrote:
    > 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]
    Robert Dober, Jul 18, 2009
    #2
    1. Advertising

  3. Intransition

    Roger Pack Guest


    > but this patch could do it if you really need it


    Is this functionality...useful enough to merit being submitted to core?
    --
    Posted via http://www.ruby-forum.com/.
    Roger Pack, Jul 18, 2009
    #3
  4. Intransition

    Robert Dober Guest

    On 7/18/09, Roger Pack <> 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

    --=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]
    Robert Dober, Jul 18, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmltIEhlYXZleQ==?=

    Char Enumerator

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Jul 16, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    381
    bruce barker
    Jul 16, 2004
  2. Angelos Karantzalis

    DataGrid: Bind to an Enumerator of object[]'s

    Angelos Karantzalis, Nov 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    438
  3. Thomas Hawtin
    Replies:
    7
    Views:
    384
    Roedy Green
    May 20, 2006
  4. Oliver Hopton

    Collection class bound to DataGrid doesn't use my custom enumerator!

    Oliver Hopton, Oct 1, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    123
    Alan Green
    Oct 29, 2003
  5. Kristof Bastiaensen

    RCR: More enumerator functionality

    Kristof Bastiaensen, May 17, 2004, in forum: Ruby
    Replies:
    10
    Views:
    288
    Kristof Bastiaensen
    May 19, 2004
Loading...

Share This Page