Skip to content

Commit fd4c17f

Browse files
committed
* enumerator.c: Add getters for receiver, method and arguments
1 parent 21cf543 commit fd4c17f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

enumerator.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,54 @@ enumerator_inspect(VALUE obj)
794794
return rb_exec_recursive(inspect_enumerator, obj, 0);
795795
}
796796

797+
/*
798+
* call-seq:
799+
* e.receiver -> obj
800+
*
801+
* Returns the object that <i>e</i> will call when iterated.
802+
*
803+
* "hello".to_enum(:gsub, /l/).receiver # => "hello"
804+
*/
805+
806+
static VALUE
807+
enumerator_receiver(VALUE obj)
808+
{
809+
return enumerator_ptr(obj)->obj;
810+
}
811+
812+
/*
813+
* call-seq:
814+
* e.method -> sym
815+
*
816+
* Returns the method that <i>e</i> will send when iterated.
817+
*
818+
* "hello".to_enum(:gsub, /l/).method # => :gsub
819+
*/
820+
821+
static VALUE
822+
enumerator_method(VALUE obj)
823+
{
824+
return ID2SYM(enumerator_ptr(obj)->meth);
825+
}
826+
827+
/*
828+
* call-seq:
829+
* e.arguments -> ary
830+
*
831+
* Returns the arguments that <i>e</i> will send when iterated.
832+
*
833+
* "hello".to_enum(:gsub, /l/).arguments # => [/l/]
834+
*/
835+
836+
static VALUE
837+
enumerator_arguments(VALUE obj)
838+
{
839+
VALUE dup, args = enumerator_ptr(obj)->args;
840+
if (!args) return rb_ary_new2(0);
841+
dup = rb_ary_new2(RARRAY_LEN(args));
842+
return rb_ary_replace(dup, args);
843+
}
844+
797845
/*
798846
* Yielder
799847
*/
@@ -1099,6 +1147,9 @@ Init_Enumerator(void)
10991147
rb_define_method(rb_cEnumerator, "feed", enumerator_feed, 1);
11001148
rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
11011149
rb_define_method(rb_cEnumerator, "inspect", enumerator_inspect, 0);
1150+
rb_define_method(rb_cEnumerator, "receiver", enumerator_receiver, 0);
1151+
rb_define_method(rb_cEnumerator, "method", enumerator_method, 0);
1152+
rb_define_method(rb_cEnumerator, "arguments", enumerator_arguments, 0);
11021153

11031154
rb_eStopIteration = rb_define_class("StopIteration", rb_eIndexError);
11041155
rb_define_method(rb_eStopIteration, "result", stop_result, 0);

0 commit comments

Comments
 (0)