Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit fd4c17f7fd2da5aa3c1fdcc46c151769eb5c7461 1 parent 21cf543
Marc-André Lafortune authored August 17, 2010

Showing 1 changed file with 51 additions and 0 deletions. Show diff stats Hide diff stats

  1. 51  enumerator.c
51  enumerator.c
@@ -795,6 +795,54 @@ enumerator_inspect(VALUE obj)
795 795
 }
796 796
 
797 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
+
  845
+/*
798 846
  * Yielder
799 847
  */
800 848
 static void
@@ -1099,6 +1147,9 @@ Init_Enumerator(void)
1099 1147
     rb_define_method(rb_cEnumerator, "feed", enumerator_feed, 1);
1100 1148
     rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
1101 1149
     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);
1102 1153
 
1103 1154
     rb_eStopIteration = rb_define_class("StopIteration", rb_eIndexError);
1104 1155
     rb_define_method(rb_eStopIteration, "result", stop_result, 0);

0 notes on commit fd4c17f

Please sign in to comment.
Something went wrong with that request. Please try again.