Permalink
Browse files

* enumerator.c: Add getters for receiver, method and arguments

  • Loading branch information...
marcandre committed Aug 18, 2010
1 parent 21cf543 commit fd4c17f7fd2da5aa3c1fdcc46c151769eb5c7461
Showing with 51 additions and 0 deletions.
  1. +51 −0 enumerator.c
View
@@ -794,6 +794,54 @@ enumerator_inspect(VALUE obj)
return rb_exec_recursive(inspect_enumerator, obj, 0);
}
+/*
+ * call-seq:
+ * e.receiver -> obj
+ *
+ * Returns the object that <i>e</i> will call when iterated.
+ *
+ * "hello".to_enum(:gsub, /l/).receiver # => "hello"
+ */
+
+static VALUE
+enumerator_receiver(VALUE obj)
+{
+ return enumerator_ptr(obj)->obj;
+}
+
+/*
+ * call-seq:
+ * e.method -> sym
+ *
+ * Returns the method that <i>e</i> will send when iterated.
+ *
+ * "hello".to_enum(:gsub, /l/).method # => :gsub
+ */
+
+static VALUE
+enumerator_method(VALUE obj)
+{
+ return ID2SYM(enumerator_ptr(obj)->meth);
+}
+
+/*
+ * call-seq:
+ * e.arguments -> ary
+ *
+ * Returns the arguments that <i>e</i> will send when iterated.
+ *
+ * "hello".to_enum(:gsub, /l/).arguments # => [/l/]
+ */
+
+static VALUE
+enumerator_arguments(VALUE obj)
+{
+ VALUE dup, args = enumerator_ptr(obj)->args;
+ if (!args) return rb_ary_new2(0);
+ dup = rb_ary_new2(RARRAY_LEN(args));
+ return rb_ary_replace(dup, args);
+}
+
/*
* Yielder
*/
@@ -1099,6 +1147,9 @@ Init_Enumerator(void)
rb_define_method(rb_cEnumerator, "feed", enumerator_feed, 1);
rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
rb_define_method(rb_cEnumerator, "inspect", enumerator_inspect, 0);
+ rb_define_method(rb_cEnumerator, "receiver", enumerator_receiver, 0);
+ rb_define_method(rb_cEnumerator, "method", enumerator_method, 0);
+ rb_define_method(rb_cEnumerator, "arguments", enumerator_arguments, 0);
rb_eStopIteration = rb_define_class("StopIteration", rb_eIndexError);
rb_define_method(rb_eStopIteration, "result", stop_result, 0);

0 comments on commit fd4c17f

Please sign in to comment.