Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Enumerator of what?

Reply
Thread Tools

Enumerator of what?

 
 
Intransition
Guest
Posts: n/a
 
      07-18-2009
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]


 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      07-18-2009
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 <(E-Mail Removed)> 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]

 
Reply With Quote
 
 
 
 
Roger Pack
Guest
Posts: n/a
 
      07-18-2009

> 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/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      07-18-2009
On 7/18/09, Roger Pack <(E-Mail Removed)> 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]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does the first enumerator have default value of 0 like in C++? Thomas Hawtin Java 7 05-20-2006 06:26 PM
Nvidia Network Bus enumerator Richard G Carruthers Windows 64bit 3 06-03-2005 05:23 PM
Hijackthis freezes on 015 Trusted Zone enumerator geermeister@gmail.com Computer Support 0 04-29-2005 03:30 PM
DataGrid: Bind to an Enumerator of object[]'s Angelos Karantzalis ASP .Net 1 11-05-2004 09:04 AM
Char Enumerator =?Utf-8?B?SmltIEhlYXZleQ==?= ASP .Net 1 07-16-2004 10:36 PM



Advertisments