Ruby  2.0.0p648(2015-12-16revision53162)
enumerator.c
Go to the documentation of this file.
1 /************************************************
2 
3  enumerator.c - provides Enumerator class
4 
5  $Author: usa $
6 
7  Copyright (C) 2001-2003 Akinori MUSHA
8 
9  $Idaemons: /home/cvs/rb/enumerator/enumerator.c,v 1.1.1.1 2001/07/15 10:12:48 knu Exp $
10  $RoughId: enumerator.c,v 1.6 2003/07/27 11:03:24 nobu Exp $
11  $Id: enumerator.c 46745 2014-07-07 03:52:52Z usa $
12 
13 ************************************************/
14 
15 #include "ruby/ruby.h"
16 #include "node.h"
17 #include "internal.h"
18 
19 /*
20  * Document-class: Enumerator
21  *
22  * A class which allows both internal and external iteration.
23  *
24  * An Enumerator can be created by the following methods.
25  * - Kernel#to_enum
26  * - Kernel#enum_for
27  * - Enumerator.new
28  *
29  * Most methods have two forms: a block form where the contents
30  * are evaluated for each item in the enumeration, and a non-block form
31  * which returns a new Enumerator wrapping the iteration.
32  *
33  * enumerator = %w(one two three).each
34  * puts enumerator.class # => Enumerator
35  *
36  * enumerator.each_with_object("foo") do |item, obj|
37  * puts "#{obj}: #{item}"
38  * end
39  *
40  * # foo: one
41  * # foo: two
42  * # foo: three
43  *
44  * enum_with_obj = enumerator.each_with_object("foo")
45  * puts enum_with_obj.class # => Enumerator
46  *
47  * enum_with_obj.each do |item, obj|
48  * puts "#{obj}: #{item}"
49  * end
50  *
51  * # foo: one
52  * # foo: two
53  * # foo: three
54  *
55  * This allows you to chain Enumerators together. For example, you
56  * can map a list's elements to strings containing the index
57  * and the element as a string via:
58  *
59  * puts %w[foo bar baz].map.with_index { |w, i| "#{i}:#{w}" }
60  * # => ["0:foo", "1:bar", "2:baz"]
61  *
62  * An Enumerator can also be used as an external iterator.
63  * For example, Enumerator#next returns the next value of the iterator
64  * or raises StopIteration if the Enumerator is at the end.
65  *
66  * e = [1,2,3].each # returns an enumerator object.
67  * puts e.next # => 1
68  * puts e.next # => 2
69  * puts e.next # => 3
70  * puts e.next # raises StopIteration
71  *
72  * You can use this to implement an internal iterator as follows:
73  *
74  * def ext_each(e)
75  * while true
76  * begin
77  * vs = e.next_values
78  * rescue StopIteration
79  * return $!.result
80  * end
81  * y = yield(*vs)
82  * e.feed y
83  * end
84  * end
85  *
86  * o = Object.new
87  *
88  * def o.each
89  * puts yield
90  * puts yield(1)
91  * puts yield(1, 2)
92  * 3
93  * end
94  *
95  * # use o.each as an internal iterator directly.
96  * puts o.each {|*x| puts x; [:b, *x] }
97  * # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
98  *
99  * # convert o.each to an external iterator for
100  * # implementing an internal iterator.
101  * puts ext_each(o.to_enum) {|*x| puts x; [:b, *x] }
102  * # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
103  *
104  */
110 
112 
113 struct enumerator {
124 };
125 
127 
128 struct generator {
130 };
131 
132 struct yielder {
134 };
135 
136 static VALUE generator_allocate(VALUE klass);
137 static VALUE generator_init(VALUE obj, VALUE proc);
138 
139 /*
140  * Enumerator
141  */
142 static void
144 {
145  struct enumerator *ptr = p;
146  rb_gc_mark(ptr->obj);
147  rb_gc_mark(ptr->args);
148  rb_gc_mark(ptr->fib);
149  rb_gc_mark(ptr->dst);
150  rb_gc_mark(ptr->lookahead);
151  rb_gc_mark(ptr->feedvalue);
152  rb_gc_mark(ptr->stop_exc);
153  rb_gc_mark(ptr->size);
154 }
155 
156 #define enumerator_free RUBY_TYPED_DEFAULT_FREE
157 
158 static size_t
159 enumerator_memsize(const void *p)
160 {
161  return p ? sizeof(struct enumerator) : 0;
162 }
163 
165  "enumerator",
166  {
170  },
171 };
172 
173 static struct enumerator *
175 {
176  struct enumerator *ptr;
177 
179  if (!ptr || ptr->obj == Qundef) {
180  rb_raise(rb_eArgError, "uninitialized enumerator");
181  }
182  return ptr;
183 }
184 
185 /*
186  * call-seq:
187  * obj.to_enum(method = :each, *args) -> enum
188  * obj.enum_for(method = :each, *args) -> enum
189  * obj.to_enum(method = :each, *args) {|*args| block} -> enum
190  * obj.enum_for(method = :each, *args){|*args| block} -> enum
191  *
192  * Creates a new Enumerator which will enumerate by calling +method+ on
193  * +obj+, passing +args+ if any.
194  *
195  * If a block is given, it will be used to calculate the size of
196  * the enumerator without the need to iterate it (see Enumerator#size).
197  *
198  * === Examples
199  *
200  * str = "xyz"
201  *
202  * enum = str.enum_for(:each_byte)
203  * enum.each { |b| puts b }
204  * # => 120
205  * # => 121
206  * # => 122
207  *
208  * # protect an array from being modified by some_method
209  * a = [1, 2, 3]
210  * some_method(a.to_enum)
211  *
212  * It is typical to call to_enum when defining methods for
213  * a generic Enumerable, in case no block is passed.
214  *
215  * Here is such an example, with parameter passing and a sizing block:
216  *
217  * module Enumerable
218  * # a generic method to repeat the values of any enumerable
219  * def repeat(n)
220  * raise ArgumentError, "#{n} is negative!" if n < 0
221  * unless block_given?
222  * return to_enum(__method__, n) do # __method__ is :repeat here
223  * sz = size # Call size and multiply by n...
224  * sz * n if sz # but return nil if size itself is nil
225  * end
226  * end
227  * each do |*val|
228  * n.times { yield *val }
229  * end
230  * end
231  * end
232  *
233  * %i[hello world].repeat(2) { |w| puts w }
234  * # => Prints 'hello', 'hello', 'world', 'world'
235  * enum = (1..14).repeat(3)
236  * # => returns an Enumerator when called without a block
237  * enum.first(4) # => [1, 1, 1, 2]
238  * enum.size # => 42
239  */
240 static VALUE
242 {
244 
245  if (argc > 0) {
246  --argc;
247  meth = *argv++;
248  }
250  if (rb_block_given_p()) {
252  }
253  return enumerator;
254 }
255 
256 static VALUE
258 {
259  struct enumerator *ptr;
260  VALUE enum_obj;
261 
262  enum_obj = TypedData_Make_Struct(klass, struct enumerator, &enumerator_data_type, ptr);
263  ptr->obj = Qundef;
264 
265  return enum_obj;
266 }
267 
268 static VALUE
270 {
271  struct enumerator *ptr;
272 
273  TypedData_Get_Struct(enum_obj, struct enumerator, &enumerator_data_type, ptr);
274 
275  if (!ptr) {
276  rb_raise(rb_eArgError, "unallocated enumerator");
277  }
278 
279  ptr->obj = obj;
280  ptr->meth = rb_to_id(meth);
281  if (argc) ptr->args = rb_ary_new4(argc, argv);
282  ptr->fib = 0;
283  ptr->dst = Qnil;
284  ptr->lookahead = Qundef;
285  ptr->feedvalue = Qundef;
286  ptr->stop_exc = Qfalse;
287  ptr->size = size;
288  ptr->size_fn = size_fn;
289 
290  return enum_obj;
291 }
292 
293 /*
294  * call-seq:
295  * Enumerator.new(size = nil) { |yielder| ... }
296  * Enumerator.new(obj, method = :each, *args)
297  *
298  * Creates a new Enumerator object, which can be used as an
299  * Enumerable.
300  *
301  * In the first form, iteration is defined by the given block, in
302  * which a "yielder" object, given as block parameter, can be used to
303  * yield a value by calling the +yield+ method (aliased as +<<+):
304  *
305  * fib = Enumerator.new do |y|
306  * a = b = 1
307  * loop do
308  * y << a
309  * a, b = b, a + b
310  * end
311  * end
312  *
313  * p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
314  *
315  * The optional parameter can be used to specify how to calculate the size
316  * in a lazy fashion (see Enumerator#size). It can either be a value or
317  * a callable object.
318  *
319  * In the second, deprecated, form, a generated Enumerator iterates over the
320  * given object using the given method with the given arguments passed.
321  *
322  * Use of this form is discouraged. Use Kernel#enum_for or Kernel#to_enum
323  * instead.
324  *
325  * e = Enumerator.new(ObjectSpace, :each_object)
326  * #-> ObjectSpace.enum_for(:each_object)
327  *
328  * e.select { |obj| obj.is_a?(Class) } #=> array of all classes
329  *
330  */
331 static VALUE
333 {
334  VALUE recv, meth = sym_each;
335  VALUE size = Qnil;
336 
337  if (rb_block_given_p()) {
338  rb_check_arity(argc, 0, 1);
340  if (argc) {
341  if (NIL_P(argv[0]) || rb_obj_is_proc(argv[0]) ||
342  (RB_TYPE_P(argv[0], T_FLOAT) && RFLOAT_VALUE(argv[0]) == INFINITY)) {
343  size = argv[0];
344  } else {
345  size = rb_to_int(argv[0]);
346  }
347  argc = 0;
348  }
349  }
350  else {
352  rb_warn("Enumerator.new without a block is deprecated; use Object#to_enum");
353  recv = *argv++;
354  if (--argc) {
355  meth = *argv++;
356  --argc;
357  }
358  }
359 
360  return enumerator_init(obj, recv, meth, argc, argv, 0, size);
361 }
362 
363 /* :nodoc: */
364 static VALUE
366 {
367  struct enumerator *ptr0, *ptr1;
368 
369  if (!OBJ_INIT_COPY(obj, orig)) return obj;
370  ptr0 = enumerator_ptr(orig);
371  if (ptr0->fib) {
372  /* Fibers cannot be copied */
373  rb_raise(rb_eTypeError, "can't copy execution context");
374  }
375 
377 
378  if (!ptr1) {
379  rb_raise(rb_eArgError, "unallocated enumerator");
380  }
381 
382  ptr1->obj = ptr0->obj;
383  ptr1->meth = ptr0->meth;
384  ptr1->args = ptr0->args;
385  ptr1->fib = 0;
386  ptr1->lookahead = Qundef;
387  ptr1->feedvalue = Qundef;
388  ptr1->size = ptr0->size;
389  ptr1->size_fn = ptr0->size_fn;
390 
391  return obj;
392 }
393 
394 /*
395  * For backwards compatibility; use rb_enumeratorize_with_size
396  */
397 VALUE
399 {
401 }
402 
403 static VALUE
405 
406 VALUE
408 {
409  /* Similar effect as calling obj.to_enum, i.e. dispatching to either
410  Kernel#to_enum vs Lazy#to_enum */
412  return lazy_to_enum_i(obj, meth, argc, argv, size_fn);
413  else
415  obj, meth, argc, argv, size_fn, Qnil);
416 }
417 
418 static VALUE
420 {
421  int argc = 0;
422  VALUE *argv = 0;
423  const struct enumerator *e = enumerator_ptr(obj);
424  ID meth = e->meth;
425 
426  if (e->args) {
427  argc = RARRAY_LENINT(e->args);
428  argv = RARRAY_PTR(e->args);
429  }
430  return rb_block_call(e->obj, meth, argc, argv, func, arg);
431 }
432 
433 /*
434  * call-seq:
435  * enum.each {...}
436  *
437  * Iterates over the block according to how this Enumerable was constructed.
438  * If no block is given, returns self.
439  *
440  */
441 static VALUE
443 {
444  if (argc > 0) {
445  struct enumerator *e = enumerator_ptr(obj = rb_obj_dup(obj));
446  VALUE args = e->args;
447  if (args) {
448  args = rb_ary_dup(args);
450  }
451  else {
453  }
454  e->args = args;
455  }
456  if (!rb_block_given_p()) return obj;
457  return enumerator_block_call(obj, 0, obj);
458 }
459 
460 static VALUE
462 {
463  NODE *memo = (NODE *)m;
464  VALUE idx = memo->u1.value;
465  memo->u1.value = rb_int_succ(idx);
466 
467  if (argc <= 1)
468  return rb_yield_values(2, val, idx);
469 
470  return rb_yield_values(2, rb_ary_new4(argc, argv), idx);
471 }
472 
473 static VALUE
475 
476 /*
477  * call-seq:
478  * e.with_index(offset = 0) {|(*args), idx| ... }
479  * e.with_index(offset = 0)
480  *
481  * Iterates the given block for each element with an index, which
482  * starts from +offset+. If no block is given, returns a new Enumerator
483  * that includes the index, starting from +offset+
484  *
485  * +offset+:: the starting index to use
486  *
487  */
488 static VALUE
490 {
491  VALUE memo;
492 
493  rb_scan_args(argc, argv, "01", &memo);
495  if (NIL_P(memo))
496  memo = INT2FIX(0);
497  else
498  memo = rb_to_int(memo);
500 }
501 
502 /*
503  * call-seq:
504  * e.each_with_index {|(*args), idx| ... }
505  * e.each_with_index
506  *
507  * Same as Enumerator#with_index(0), i.e. there is no starting offset.
508  *
509  * If no block is given, a new Enumerator is returned that includes the index.
510  *
511  */
512 static VALUE
514 {
515  return enumerator_with_index(0, NULL, obj);
516 }
517 
518 static VALUE
520 {
521  if (argc <= 1)
522  return rb_yield_values(2, val, memo);
523 
524  return rb_yield_values(2, rb_ary_new4(argc, argv), memo);
525 }
526 
527 /*
528  * call-seq:
529  * e.with_object(obj) {|(*args), obj| ... }
530  * e.with_object(obj)
531  *
532  * Iterates the given block for each element with an arbitrary object, +obj+,
533  * and returns +obj+
534  *
535  * If no block is given, returns a new Enumerator.
536  *
537  * === Example
538  *
539  * to_three = Enumerator.new do |y|
540  * 3.times do |x|
541  * y << x
542  * end
543  * end
544  *
545  * to_three_with_string = to_three.with_object("foo")
546  * to_three_with_string.each do |x,string|
547  * puts "#{string}: #{x}"
548  * end
549  *
550  * # => foo:0
551  * # => foo:1
552  * # => foo:2
553  */
554 static VALUE
556 {
559 
560  return memo;
561 }
562 
563 static VALUE
565 {
566  struct enumerator *e = enumerator_ptr(obj);
567  VALUE feedvalue = Qnil;
569  rb_fiber_yield(1, &args);
570  if (e->feedvalue != Qundef) {
571  feedvalue = e->feedvalue;
572  e->feedvalue = Qundef;
573  }
574  return feedvalue;
575 }
576 
577 static VALUE
579 {
580  struct enumerator *e = enumerator_ptr(obj);
581  VALUE nil = Qnil;
582  VALUE result;
583 
585  e->stop_exc = rb_exc_new2(rb_eStopIteration, "iteration reached an end");
587  return rb_fiber_yield(1, &nil);
588 }
589 
590 static void
592 {
593  VALUE curr = rb_fiber_current();
594  e->dst = curr;
595  e->fib = rb_fiber_new(next_i, obj);
596  e->lookahead = Qundef;
597 }
598 
599 static VALUE
601 {
602  VALUE curr, vs;
603 
604  if (e->stop_exc)
606 
607  curr = rb_fiber_current();
608 
609  if (!e->fib || !rb_fiber_alive_p(e->fib)) {
610  next_init(obj, e);
611  }
612 
613  vs = rb_fiber_resume(e->fib, 1, &curr);
614  if (e->stop_exc) {
615  e->fib = 0;
616  e->dst = Qnil;
617  e->lookahead = Qundef;
618  e->feedvalue = Qundef;
620  }
621  return vs;
622 }
623 
624 /*
625  * call-seq:
626  * e.next_values -> array
627  *
628  * Returns the next object as an array in the enumerator, and move the
629  * internal position forward. When the position reached at the end,
630  * StopIteration is raised.
631  *
632  * This method can be used to distinguish <code>yield</code> and <code>yield
633  * nil</code>.
634  *
635  * === Example
636  *
637  * o = Object.new
638  * def o.each
639  * yield
640  * yield 1
641  * yield 1, 2
642  * yield nil
643  * yield [1, 2]
644  * end
645  * e = o.to_enum
646  * p e.next_values
647  * p e.next_values
648  * p e.next_values
649  * p e.next_values
650  * p e.next_values
651  * e = o.to_enum
652  * p e.next
653  * p e.next
654  * p e.next
655  * p e.next
656  * p e.next
657  *
658  * ## yield args next_values next
659  * # yield [] nil
660  * # yield 1 [1] 1
661  * # yield 1, 2 [1, 2] [1, 2]
662  * # yield nil [nil] nil
663  * # yield [1, 2] [[1, 2]] [1, 2]
664  *
665  * Note that +next_values+ does not affect other non-external enumeration
666  * methods unless underlying iteration method itself has side-effect, e.g.
667  * IO#each_line.
668  *
669  */
670 
671 static VALUE
673 {
674  struct enumerator *e = enumerator_ptr(obj);
675  VALUE vs;
676 
677  if (e->lookahead != Qundef) {
678  vs = e->lookahead;
679  e->lookahead = Qundef;
680  return vs;
681  }
682 
683  return get_next_values(obj, e);
684 }
685 
686 static VALUE
687 ary2sv(VALUE args, int dup)
688 {
689  if (!RB_TYPE_P(args, T_ARRAY))
690  return args;
691 
692  switch (RARRAY_LEN(args)) {
693  case 0:
694  return Qnil;
695 
696  case 1:
697  return RARRAY_PTR(args)[0];
698 
699  default:
700  if (dup)
701  return rb_ary_dup(args);
702  return args;
703  }
704 }
705 
706 /*
707  * call-seq:
708  * e.next -> object
709  *
710  * Returns the next object in the enumerator, and move the internal position
711  * forward. When the position reached at the end, StopIteration is raised.
712  *
713  * === Example
714  *
715  * a = [1,2,3]
716  * e = a.to_enum
717  * p e.next #=> 1
718  * p e.next #=> 2
719  * p e.next #=> 3
720  * p e.next #raises StopIteration
721  *
722  * Note that enumeration sequence by +next+ does not affect other non-external
723  * enumeration methods, unless the underlying iteration methods itself has
724  * side-effect, e.g. IO#each_line.
725  *
726  */
727 
728 static VALUE
730 {
732  return ary2sv(vs, 0);
733 }
734 
735 static VALUE
737 {
738  struct enumerator *e = enumerator_ptr(obj);
739 
740  if (e->lookahead == Qundef) {
741  e->lookahead = get_next_values(obj, e);
742  }
743  return e->lookahead;
744 }
745 
746 /*
747  * call-seq:
748  * e.peek_values -> array
749  *
750  * Returns the next object as an array, similar to Enumerator#next_values, but
751  * doesn't move the internal position forward. If the position is already at
752  * the end, StopIteration is raised.
753  *
754  * === Example
755  *
756  * o = Object.new
757  * def o.each
758  * yield
759  * yield 1
760  * yield 1, 2
761  * end
762  * e = o.to_enum
763  * p e.peek_values #=> []
764  * e.next
765  * p e.peek_values #=> [1]
766  * p e.peek_values #=> [1]
767  * e.next
768  * p e.peek_values #=> [1, 2]
769  * e.next
770  * p e.peek_values # raises StopIteration
771  *
772  */
773 
774 static VALUE
776 {
778 }
779 
780 /*
781  * call-seq:
782  * e.peek -> object
783  *
784  * Returns the next object in the enumerator, but doesn't move the internal
785  * position forward. If the position is already at the end, StopIteration
786  * is raised.
787  *
788  * === Example
789  *
790  * a = [1,2,3]
791  * e = a.to_enum
792  * p e.next #=> 1
793  * p e.peek #=> 2
794  * p e.peek #=> 2
795  * p e.peek #=> 2
796  * p e.next #=> 2
797  * p e.next #=> 3
798  * p e.peek #raises StopIteration
799  *
800  */
801 
802 static VALUE
804 {
806  return ary2sv(vs, 1);
807 }
808 
809 /*
810  * call-seq:
811  * e.feed obj -> nil
812  *
813  * Sets the value to be returned by the next yield inside +e+.
814  *
815  * If the value is not set, the yield returns nil.
816  *
817  * This value is cleared after being yielded.
818  *
819  * o = Object.new
820  * def o.each
821  * x = yield # (2) blocks
822  * p x # (5) => "foo"
823  * x = yield # (6) blocks
824  * p x # (8) => nil
825  * x = yield # (9) blocks
826  * p x # not reached w/o another e.next
827  * end
828  *
829  * e = o.to_enum
830  * e.next # (1)
831  * e.feed "foo" # (3)
832  * e.next # (4)
833  * e.next # (7)
834  * # (10)
835  */
836 
837 static VALUE
839 {
840  struct enumerator *e = enumerator_ptr(obj);
841 
842  if (e->feedvalue != Qundef) {
843  rb_raise(rb_eTypeError, "feed value already set");
844  }
845  e->feedvalue = v;
846 
847  return Qnil;
848 }
849 
850 /*
851  * call-seq:
852  * e.rewind -> e
853  *
854  * Rewinds the enumeration sequence to the beginning.
855  *
856  * If the enclosed object responds to a "rewind" method, it is called.
857  */
858 
859 static VALUE
861 {
862  struct enumerator *e = enumerator_ptr(obj);
863 
864  rb_check_funcall(e->obj, id_rewind, 0, 0);
865 
866  e->fib = 0;
867  e->dst = Qnil;
868  e->lookahead = Qundef;
869  e->feedvalue = Qundef;
870  e->stop_exc = Qfalse;
871  return obj;
872 }
873 
874 static VALUE
876 {
877  struct enumerator *e;
878  const char *cname;
879  VALUE eobj, eargs, str, method;
880  int tainted, untrusted;
881 
883 
884  cname = rb_obj_classname(obj);
885 
886  if (!e || e->obj == Qundef) {
887  return rb_sprintf("#<%s: uninitialized>", cname);
888  }
889 
890  if (recur) {
891  str = rb_sprintf("#<%s: ...>", cname);
892  OBJ_TAINT(str);
893  return str;
894  }
895 
896  eobj = rb_attr_get(obj, id_receiver);
897  if (NIL_P(eobj)) {
898  eobj = e->obj;
899  }
900 
901  tainted = OBJ_TAINTED(eobj);
902  untrusted = OBJ_UNTRUSTED(eobj);
903 
904  /* (1..100).each_cons(2) => "#<Enumerator: 1..100:each_cons(2)>" */
905  str = rb_sprintf("#<%s: ", cname);
906  rb_str_concat(str, rb_inspect(eobj));
907  method = rb_attr_get(obj, id_method);
908  if (NIL_P(method)) {
909  rb_str_buf_cat2(str, ":");
910  rb_str_buf_cat2(str, rb_id2name(e->meth));
911  }
912  else if (method != Qfalse) {
913  Check_Type(method, T_SYMBOL);
914  rb_str_buf_cat2(str, ":");
915  rb_str_buf_cat2(str, rb_id2name(SYM2ID(method)));
916  }
917 
918  eargs = rb_attr_get(obj, id_arguments);
919  if (NIL_P(eargs)) {
920  eargs = e->args;
921  }
922  if (eargs != Qfalse) {
923  long argc = RARRAY_LEN(eargs);
924  VALUE *argv = RARRAY_PTR(eargs);
925 
926  if (argc > 0) {
927  rb_str_buf_cat2(str, "(");
928 
929  while (argc--) {
930  VALUE arg = *argv++;
931 
932  rb_str_concat(str, rb_inspect(arg));
933  rb_str_buf_cat2(str, argc > 0 ? ", " : ")");
934 
935  if (OBJ_TAINTED(arg)) tainted = TRUE;
936  if (OBJ_UNTRUSTED(arg)) untrusted = TRUE;
937  }
938  }
939  }
940 
941  rb_str_buf_cat2(str, ">");
942 
943  if (tainted) OBJ_TAINT(str);
944  if (untrusted) OBJ_UNTRUST(str);
945  return str;
946 }
947 
948 /*
949  * call-seq:
950  * e.inspect -> string
951  *
952  * Creates a printable version of <i>e</i>.
953  */
954 
955 static VALUE
957 {
959 }
960 
961 /*
962  * call-seq:
963  * e.size -> int, Float::INFINITY or nil
964  *
965  * Returns the size of the enumerator, or +nil+ if it can't be calculated lazily.
966  *
967  * (1..100).to_a.permutation(4).size # => 94109400
968  * loop.size # => Float::INFINITY
969  * (1..100).drop_while.size # => nil
970  */
971 
972 static VALUE
974 {
975  struct enumerator *e = enumerator_ptr(obj);
976 
977  if (e->size_fn) {
978  return (*e->size_fn)(e->obj, e->args, obj);
979  }
980  if (rb_obj_is_proc(e->size)) {
981  if (e->args)
982  return rb_proc_call(e->size, e->args);
983  else
984  return rb_proc_call_with_block(e->size, 0, 0, Qnil);
985  }
986  return e->size;
987 }
988 
989 /*
990  * Yielder
991  */
992 static void
994 {
995  struct yielder *ptr = p;
996  rb_gc_mark(ptr->proc);
997 }
998 
999 #define yielder_free RUBY_TYPED_DEFAULT_FREE
1000 
1001 static size_t
1002 yielder_memsize(const void *p)
1003 {
1004  return p ? sizeof(struct yielder) : 0;
1005 }
1006 
1008  "yielder",
1009  {
1010  yielder_mark,
1011  yielder_free,
1013  },
1014 };
1015 
1016 static struct yielder *
1018 {
1019  struct yielder *ptr;
1020 
1021  TypedData_Get_Struct(obj, struct yielder, &yielder_data_type, ptr);
1022  if (!ptr || ptr->proc == Qundef) {
1023  rb_raise(rb_eArgError, "uninitialized yielder");
1024  }
1025  return ptr;
1026 }
1027 
1028 /* :nodoc: */
1029 static VALUE
1031 {
1032  struct yielder *ptr;
1033  VALUE obj;
1034 
1035  obj = TypedData_Make_Struct(klass, struct yielder, &yielder_data_type, ptr);
1036  ptr->proc = Qundef;
1037 
1038  return obj;
1039 }
1040 
1041 static VALUE
1043 {
1044  struct yielder *ptr;
1045 
1046  TypedData_Get_Struct(obj, struct yielder, &yielder_data_type, ptr);
1047 
1048  if (!ptr) {
1049  rb_raise(rb_eArgError, "unallocated yielder");
1050  }
1051 
1052  ptr->proc = proc;
1053 
1054  return obj;
1055 }
1056 
1057 /* :nodoc: */
1058 static VALUE
1060 {
1061  rb_need_block();
1062 
1063  return yielder_init(obj, rb_block_proc());
1064 }
1065 
1066 /* :nodoc: */
1067 static VALUE
1069 {
1070  struct yielder *ptr = yielder_ptr(obj);
1071 
1072  return rb_proc_call(ptr->proc, args);
1073 }
1074 
1075 /* :nodoc: */
1077 {
1078  yielder_yield(obj, args);
1079  return obj;
1080 }
1081 
1082 static VALUE
1084 {
1085  return rb_yield_values2(argc, argv);
1086 }
1087 
1088 static VALUE
1090 {
1092 }
1093 
1094 /*
1095  * Generator
1096  */
1097 static void
1099 {
1100  struct generator *ptr = p;
1101  rb_gc_mark(ptr->proc);
1102 }
1103 
1104 #define generator_free RUBY_TYPED_DEFAULT_FREE
1105 
1106 static size_t
1107 generator_memsize(const void *p)
1108 {
1109  return p ? sizeof(struct generator) : 0;
1110 }
1111 
1113  "generator",
1114  {
1118  },
1119 };
1120 
1121 static struct generator *
1123 {
1124  struct generator *ptr;
1125 
1127  if (!ptr || ptr->proc == Qundef) {
1128  rb_raise(rb_eArgError, "uninitialized generator");
1129  }
1130  return ptr;
1131 }
1132 
1133 /* :nodoc: */
1134 static VALUE
1136 {
1137  struct generator *ptr;
1138  VALUE obj;
1139 
1140  obj = TypedData_Make_Struct(klass, struct generator, &generator_data_type, ptr);
1141  ptr->proc = Qundef;
1142 
1143  return obj;
1144 }
1145 
1146 static VALUE
1148 {
1149  struct generator *ptr;
1150 
1152 
1153  if (!ptr) {
1154  rb_raise(rb_eArgError, "unallocated generator");
1155  }
1156 
1157  ptr->proc = proc;
1158 
1159  return obj;
1160 }
1161 
1162 /* :nodoc: */
1163 static VALUE
1165 {
1166  VALUE proc;
1167 
1168  if (argc == 0) {
1169  rb_need_block();
1170 
1171  proc = rb_block_proc();
1172  }
1173  else {
1174  rb_scan_args(argc, argv, "1", &proc);
1175 
1176  if (!rb_obj_is_proc(proc))
1178  "wrong argument type %s (expected Proc)",
1180 
1181  if (rb_block_given_p()) {
1182  rb_warn("given block not used");
1183  }
1184  }
1185 
1186  return generator_init(obj, proc);
1187 }
1188 
1189 /* :nodoc: */
1190 static VALUE
1192 {
1193  struct generator *ptr0, *ptr1;
1194 
1195  if (!OBJ_INIT_COPY(obj, orig)) return obj;
1196 
1197  ptr0 = generator_ptr(orig);
1198 
1199  TypedData_Get_Struct(obj, struct generator, &generator_data_type, ptr1);
1200 
1201  if (!ptr1) {
1202  rb_raise(rb_eArgError, "unallocated generator");
1203  }
1204 
1205  ptr1->proc = ptr0->proc;
1206 
1207  return obj;
1208 }
1209 
1210 /* :nodoc: */
1211 static VALUE
1213 {
1214  struct generator *ptr = generator_ptr(obj);
1215  VALUE args = rb_ary_new2(argc + 1);
1216 
1218  if (argc > 0) {
1219  rb_ary_cat(args, argv, argc);
1220  }
1221 
1222  return rb_proc_call(ptr->proc, args);
1223 }
1224 
1225 /* Lazy Enumerator methods */
1226 static VALUE
1228 {
1229  VALUE r = rb_check_funcall(self, id_size, 0, 0);
1230  return (r == Qundef) ? Qnil : r;
1231 }
1232 
1233 static VALUE
1235 {
1236  return enum_size(rb_ivar_get(self, id_receiver));
1237 }
1238 
1239 static VALUE
1241 {
1242  return lazy_size(lazy);
1243 }
1244 
1245 static VALUE
1247 {
1248  VALUE result;
1249  if (argc == 1) {
1250  VALUE args[2];
1251  args[0] = m;
1252  args[1] = val;
1254  }
1255  else {
1256  VALUE args;
1257  int len = rb_long2int((long)argc + 1);
1258 
1259  args = rb_ary_tmp_new(len);
1260  rb_ary_push(args, m);
1261  if (argc > 0) {
1262  rb_ary_cat(args, argv, argc);
1263  }
1265  RB_GC_GUARD(args);
1266  }
1267  if (result == Qundef) rb_iter_break();
1268  return Qnil;
1269 }
1270 
1271 static VALUE
1273 {
1275  return Qnil;
1276 }
1277 
1278 /*
1279  * call-seq:
1280  * Lazy.new(obj, size=nil) { |yielder, *values| ... }
1281  *
1282  * Creates a new Lazy enumerator. When the enumerator is actually enumerated
1283  * (e.g. by calling #force), +obj+ will be enumerated and each value passed
1284  * to the given block. The block can yield values back using +yielder+.
1285  * For example, to create a method +filter_map+ in both lazy and
1286  * non-lazy fashions:
1287  *
1288  * module Enumerable
1289  * def filter_map(&block)
1290  * map(&block).compact
1291  * end
1292  * end
1293  *
1294  * class Enumerator::Lazy
1295  * def filter_map
1296  * Lazy.new(self) do |yielder, *values|
1297  * result = yield *values
1298  * yielder << result if result
1299  * end
1300  * end
1301  * end
1302  *
1303  * (1..Float::INFINITY).lazy.filter_map{|i| i*i if i.even?}.first(5)
1304  * # => [4, 16, 36, 64, 100]
1305  */
1306 static VALUE
1308 {
1309  VALUE obj, size = Qnil;
1310  VALUE generator;
1311 
1312  rb_check_arity(argc, 1, 2);
1313  if (!rb_block_given_p()) {
1314  rb_raise(rb_eArgError, "tried to call lazy new without a block");
1315  }
1316  obj = argv[0];
1317  if (argc > 1) {
1318  size = argv[1];
1319  }
1322  enumerator_init(self, generator, sym_each, 0, 0, 0, size);
1323  rb_ivar_set(self, id_receiver, obj);
1324 
1325  return self;
1326 }
1327 
1328 static VALUE
1330 {
1331  ID id = rb_frame_this_func();
1332  struct enumerator *e = enumerator_ptr(lazy);
1333  rb_ivar_set(lazy, id_method, ID2SYM(id));
1334  if (NIL_P(args)) {
1335  /* Qfalse indicates that the arguments are empty */
1337  }
1338  else {
1339  rb_ivar_set(lazy, id_arguments, args);
1340  }
1341  e->size_fn = size_fn;
1342  return lazy;
1343 }
1344 
1345 /*
1346  * call-seq:
1347  * e.lazy -> lazy_enumerator
1348  *
1349  * Returns a lazy enumerator, whose methods map/collect,
1350  * flat_map/collect_concat, select/find_all, reject, grep, zip, take,
1351  * take_while, drop, and drop_while enumerate values only on an
1352  * as-needed basis. However, if a block is given to zip, values
1353  * are enumerated immediately.
1354  *
1355  * === Example
1356  *
1357  * The following program finds pythagorean triples:
1358  *
1359  * def pythagorean_triples
1360  * (1..Float::INFINITY).lazy.flat_map {|z|
1361  * (1..z).flat_map {|x|
1362  * (x..z).select {|y|
1363  * x**2 + y**2 == z**2
1364  * }.map {|y|
1365  * [x, y, z]
1366  * }
1367  * }
1368  * }
1369  * end
1370  * # show first ten pythagorean triples
1371  * p pythagorean_triples.take(10).force # take is lazy, so force is needed
1372  * p pythagorean_triples.first(10) # first is eager
1373  * # show pythagorean triples less than 100
1374  * p pythagorean_triples.take_while { |*, z| z < 100 }.force
1375  */
1376 static VALUE
1378 {
1380  /* Qfalse indicates that the Enumerator::Lazy has no method name */
1382  return result;
1383 }
1384 
1385 static VALUE
1387 {
1389  obj, meth, argc, argv, size_fn, Qnil);
1390 }
1391 
1392 /*
1393  * call-seq:
1394  * lzy.to_enum(method = :each, *args) -> lazy_enum
1395  * lzy.enum_for(method = :each, *args) -> lazy_enum
1396  * lzy.to_enum(method = :each, *args) {|*args| block} -> lazy_enum
1397  * lzy.enum_for(method = :each, *args){|*args| block} -> lazy_enum
1398  *
1399  * Similar to Kernel#to_enum, except it returns a lazy enumerator.
1400  * This makes it easy to define Enumerable methods that will
1401  * naturally remain lazy if called from a lazy enumerator.
1402  *
1403  * For example, continuing from the example in Kernel#to_enum:
1404  *
1405  * # See Kernel#to_enum for the definition of repeat
1406  * r = 1..Float::INFINITY
1407  * r.repeat(2).first(5) # => [1, 1, 2, 2, 3]
1408  * r.repeat(2).class # => Enumerator
1409  * r.repeat(2).map{|n| n ** 2}.first(5) # => endless loop!
1410  * # works naturally on lazy enumerator:
1411  * r.lazy.repeat(2).class # => Enumerator::Lazy
1412  * r.lazy.repeat(2).map{|n| n ** 2}.first(5) # => [1, 1, 4, 4, 9]
1413  */
1414 
1415 static VALUE
1417 {
1418  VALUE lazy, meth = sym_each;
1419 
1420  if (argc > 0) {
1421  --argc;
1422  meth = *argv++;
1423  }
1424  lazy = lazy_to_enum_i(self, meth, argc, argv, 0);
1425  if (rb_block_given_p()) {
1426  enumerator_ptr(lazy)->size = rb_block_proc();
1427  }
1428  return lazy;
1429 }
1430 
1431 static VALUE
1433 {
1434  VALUE result = rb_yield_values2(argc - 1, &argv[1]);
1435 
1436  rb_funcall(argv[0], id_yield, 1, result);
1437  return Qnil;
1438 }
1439 
1440 static VALUE
1442 {
1443  if (!rb_block_given_p()) {
1444  rb_raise(rb_eArgError, "tried to call lazy map without a block");
1445  }
1446 
1448  lazy_map_func, 0),
1450 }
1451 
1452 static VALUE
1454 {
1455  return rb_funcall2(yielder, id_yield, argc, argv);
1456 }
1457 
1458 static VALUE
1460 {
1462  return Qnil;
1463 }
1464 
1465 static VALUE
1467 {
1468  VALUE ary = rb_check_array_type(obj);
1469  if (NIL_P(ary)) {
1471  }
1472  else {
1473  long i;
1474  for (i = 0; i < RARRAY_LEN(ary); i++) {
1475  rb_funcall(yielder, id_yield, 1, RARRAY_PTR(ary)[i]);
1476  }
1477  }
1478  return Qnil;
1479 }
1480 
1481 static VALUE
1483 {
1484  VALUE result = rb_yield_values2(argc - 1, &argv[1]);
1485  if (RB_TYPE_P(result, T_ARRAY)) {
1486  long i;
1487  for (i = 0; i < RARRAY_LEN(result); i++) {
1489  }
1490  }
1491  else {
1494  }
1495  else {
1497  }
1498  }
1499  return Qnil;
1500 }
1501 
1502 /*
1503  * call-seq:
1504  * lazy.flat_map { |obj| block } -> a_lazy_enumerator
1505  *
1506  * Returns a new lazy enumerator with the concatenated results of running
1507  * <i>block</i> once for every element in <i>lazy</i>.
1508  *
1509  * ["foo", "bar"].lazy.flat_map {|i| i.each_char.lazy}.force
1510  * #=> ["f", "o", "o", "b", "a", "r"]
1511  *
1512  * A value <i>x</i> returned by <i>block</i> is decomposed if either of
1513  * the following conditions is true:
1514  *
1515  * a) <i>x</i> responds to both each and force, which means that
1516  * <i>x</i> is a lazy enumerator.
1517  * b) <i>x</i> is an array or responds to to_ary.
1518  *
1519  * Otherwise, <i>x</i> is contained as-is in the return value.
1520  *
1521  * [{a:1}, {b:2}].lazy.flat_map {|i| i}.force
1522  * #=> [{:a=>1}, {:b=>2}]
1523  */
1524 static VALUE
1526 {
1527  if (!rb_block_given_p()) {
1528  rb_raise(rb_eArgError, "tried to call lazy flat_map without a block");
1529  }
1530 
1532  lazy_flat_map_func, 0),
1533  Qnil, 0);
1534 }
1535 
1536 static VALUE
1538 {
1539  VALUE element = rb_enum_values_pack(argc - 1, argv + 1);
1540 
1541  if (RTEST(rb_yield(element))) {
1542  return rb_funcall(argv[0], id_yield, 1, element);
1543  }
1544  return Qnil;
1545 }
1546 
1547 static VALUE
1549 {
1550  if (!rb_block_given_p()) {
1551  rb_raise(rb_eArgError, "tried to call lazy select without a block");
1552  }
1553 
1555  lazy_select_func, 0),
1556  Qnil, 0);
1557 }
1558 
1559 static VALUE
1561 {
1562  VALUE element = rb_enum_values_pack(argc - 1, argv + 1);
1563 
1564  if (!RTEST(rb_yield(element))) {
1565  return rb_funcall(argv[0], id_yield, 1, element);
1566  }
1567  return Qnil;
1568 }
1569 
1570 static VALUE
1572 {
1573  if (!rb_block_given_p()) {
1574  rb_raise(rb_eArgError, "tried to call lazy reject without a block");
1575  }
1576 
1578  lazy_reject_func, 0),
1579  Qnil, 0);
1580 }
1581 
1582 static VALUE
1584 {
1585  VALUE i = rb_enum_values_pack(argc - 1, argv + 1);
1586  VALUE result = rb_funcall(m, id_eqq, 1, i);
1587 
1588  if (RTEST(result)) {
1589  rb_funcall(argv[0], id_yield, 1, i);
1590  }
1591  return Qnil;
1592 }
1593 
1594 static VALUE
1596 {
1597  VALUE i = rb_enum_values_pack(argc - 1, argv + 1);
1598  VALUE result = rb_funcall(m, id_eqq, 1, i);
1599 
1600  if (RTEST(result)) {
1601  rb_funcall(argv[0], id_yield, 1, rb_yield(i));
1602  }
1603  return Qnil;
1604 }
1605 
1606 static VALUE
1608 {
1610  rb_block_given_p() ?
1612  pattern),
1613  rb_ary_new3(1, pattern), 0);
1614 }
1615 
1616 static VALUE
1618 {
1619  return rb_funcall(obj, id_next, 0);
1620 }
1621 
1622 static VALUE
1624 {
1625  return Qnil;
1626 }
1627 
1628 static VALUE
1630 {
1631  VALUE yielder, ary, memo;
1632  long i, count;
1633 
1634  yielder = argv[0];
1635  memo = rb_attr_get(yielder, id_memo);
1636  count = NIL_P(memo) ? 0 : NUM2LONG(memo);
1637 
1638  ary = rb_ary_new2(RARRAY_LEN(arrays) + 1);
1639  rb_ary_push(ary, argv[1]);
1640  for (i = 0; i < RARRAY_LEN(arrays); i++) {
1641  rb_ary_push(ary, rb_ary_entry(RARRAY_PTR(arrays)[i], count));
1642  }
1643  rb_funcall(yielder, id_yield, 1, ary);
1645  return Qnil;
1646 }
1647 
1648 static VALUE
1650 {
1651  VALUE yielder, ary, arg, v;
1652  long i;
1653 
1654  yielder = argv[0];
1655  arg = rb_attr_get(yielder, id_memo);
1656  if (NIL_P(arg)) {
1657  arg = rb_ary_new2(RARRAY_LEN(zip_args));
1658  for (i = 0; i < RARRAY_LEN(zip_args); i++) {
1659  rb_ary_push(arg, rb_funcall(RARRAY_PTR(zip_args)[i], id_to_enum, 0));
1660  }
1661  rb_ivar_set(yielder, id_memo, arg);
1662  }
1663 
1664  ary = rb_ary_new2(RARRAY_LEN(arg) + 1);
1665  v = Qnil;
1666  if (--argc > 0) {
1667  ++argv;
1668  v = argc > 1 ? rb_ary_new4(argc, argv) : *argv;
1669  }
1670  rb_ary_push(ary, v);
1671  for (i = 0; i < RARRAY_LEN(arg); i++) {
1673  rb_eStopIteration, (VALUE)0);
1674  rb_ary_push(ary, v);
1675  }
1676  rb_funcall(yielder, id_yield, 1, ary);
1677  return Qnil;
1678 }
1679 
1680 static VALUE
1682 {
1683  VALUE ary, v;
1684  long i;
1686 
1687  if (rb_block_given_p()) {
1688  return rb_call_super(argc, argv);
1689  }
1690 
1691  ary = rb_ary_new2(argc);
1692  for (i = 0; i < argc; i++) {
1694  if (NIL_P(v)) {
1695  for (; i < argc; i++) {
1696  if (!rb_respond_to(argv[i], id_each)) {
1697  rb_raise(rb_eTypeError, "wrong argument type %s (must respond to :each)",
1698  rb_obj_classname(argv[i]));
1699  }
1700  }
1701  ary = rb_ary_new4(argc, argv);
1702  func = lazy_zip_func;
1703  break;
1704  }
1705  rb_ary_push(ary, v);
1706  }
1707 
1709  func, ary),
1710  ary, lazy_receiver_size);
1711 }
1712 
1713 static VALUE
1715 {
1716  long remain;
1717  VALUE memo = rb_attr_get(argv[0], id_memo);
1718  if (NIL_P(memo)) {
1719  memo = args;
1720  }
1721 
1722  rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
1723  if ((remain = NUM2LONG(memo)-1) == 0) {
1724  return Qundef;
1725  }
1726  else {
1727  rb_ivar_set(argv[0], id_memo, LONG2NUM(remain));
1728  return Qnil;
1729  }
1730 }
1731 
1732 static VALUE
1734 {
1735  VALUE receiver = lazy_size(lazy);
1736  long len = NUM2LONG(RARRAY_PTR(rb_ivar_get(lazy, id_arguments))[0]);
1737  if (NIL_P(receiver) || (FIXNUM_P(receiver) && FIX2LONG(receiver) < len))
1738  return receiver;
1739  return LONG2NUM(len);
1740 }
1741 
1742 static VALUE
1744 {
1745  long len = NUM2LONG(n);
1746  VALUE lazy;
1747 
1748  if (len < 0) {
1749  rb_raise(rb_eArgError, "attempt to take negative size");
1750  }
1751  if (len == 0) {
1752  VALUE len = INT2NUM(0);
1753  lazy = lazy_to_enum_i(obj, sym_cycle, 1, &len, 0);
1754  }
1755  else {
1756  lazy = rb_block_call(rb_cLazy, id_new, 1, &obj,
1757  lazy_take_func, n);
1758  }
1759  return lazy_set_method(lazy, rb_ary_new3(1, n), lazy_take_size);
1760 }
1761 
1762 static VALUE
1764 {
1765  VALUE result = rb_yield_values2(argc - 1, &argv[1]);
1766  if (!RTEST(result)) return Qundef;
1767  rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
1768  return Qnil;
1769 }
1770 
1771 static VALUE
1773 {
1774  if (!rb_block_given_p()) {
1775  rb_raise(rb_eArgError, "tried to call lazy take_while without a block");
1776  }
1779  Qnil, 0);
1780 }
1781 
1782 static VALUE
1784 {
1785  long len = NUM2LONG(RARRAY_PTR(rb_ivar_get(lazy, id_arguments))[0]);
1786  VALUE receiver = lazy_size(lazy);
1787  if (NIL_P(receiver))
1788  return receiver;
1789  if (FIXNUM_P(receiver)) {
1790  len = FIX2LONG(receiver) - len;
1791  return LONG2FIX(len < 0 ? 0 : len);
1792  }
1793  return rb_funcall(receiver, '-', 1, LONG2NUM(len));
1794 }
1795 
1796 static VALUE
1798 {
1799  long remain;
1800  VALUE memo = rb_attr_get(argv[0], id_memo);
1801  if (NIL_P(memo)) {
1802  memo = args;
1803  }
1804  if ((remain = NUM2LONG(memo)) == 0) {
1805  rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
1806  }
1807  else {
1808  rb_ivar_set(argv[0], id_memo, LONG2NUM(--remain));
1809  }
1810  return Qnil;
1811 }
1812 
1813 static VALUE
1815 {
1816  long len = NUM2LONG(n);
1817 
1818  if (len < 0) {
1819  rb_raise(rb_eArgError, "attempt to drop negative size");
1820  }
1822  lazy_drop_func, n),
1823  rb_ary_new3(1, n), lazy_drop_size);
1824 }
1825 
1826 static VALUE
1828 {
1829  VALUE memo = rb_attr_get(argv[0], id_memo);
1830  if (NIL_P(memo) && !RTEST(rb_yield_values2(argc - 1, &argv[1]))) {
1831  rb_ivar_set(argv[0], id_memo, memo = Qtrue);
1832  }
1833  if (memo == Qtrue) {
1834  rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
1835  }
1836  return Qnil;
1837 }
1838 
1839 static VALUE
1841 {
1842  if (!rb_block_given_p()) {
1843  rb_raise(rb_eArgError, "tried to call lazy drop_while without a block");
1844  }
1847  Qnil, 0);
1848 }
1849 
1850 static VALUE
1852 {
1854 }
1855 
1856 static VALUE
1858 {
1859  return obj;
1860 }
1861 
1862 /*
1863  * Document-class: StopIteration
1864  *
1865  * Raised to stop the iteration, in particular by Enumerator#next. It is
1866  * rescued by Kernel#loop.
1867  *
1868  * loop do
1869  * puts "Hello"
1870  * raise StopIteration
1871  * puts "World"
1872  * end
1873  * puts "Done!"
1874  *
1875  * <em>produces:</em>
1876  *
1877  * Hello
1878  * Done!
1879  */
1880 
1881 /*
1882  * call-seq:
1883  * result -> value
1884  *
1885  * Returns the return value of the iterator.
1886  *
1887  * o = Object.new
1888  * def o.each
1889  * yield 1
1890  * yield 2
1891  * yield 3
1892  * 100
1893  * end
1894  *
1895  * e = o.to_enum
1896  *
1897  * puts e.next #=> 1
1898  * puts e.next #=> 2
1899  * puts e.next #=> 3
1900  *
1901  * begin
1902  * e.next
1903  * rescue StopIteration => ex
1904  * puts ex.result #=> 100
1905  * end
1906  *
1907  */
1908 
1909 static VALUE
1911 {
1912  return rb_attr_get(self, id_result);
1913 }
1914 
1915 void
1917 {
1918  rb_define_method(rb_mKernel, "to_enum", obj_to_enum, -1);
1919  rb_define_method(rb_mKernel, "enum_for", obj_to_enum, -1);
1920 
1921  rb_cEnumerator = rb_define_class("Enumerator", rb_cObject);
1923 
1926  rb_define_method(rb_cEnumerator, "initialize_copy", enumerator_init_copy, 1);
1929  rb_define_method(rb_cEnumerator, "each_with_object", enumerator_with_object, 1);
1940 
1941  /* Lazy */
1944  rb_define_method(rb_cLazy, "initialize", lazy_initialize, -1);
1945  rb_define_method(rb_cLazy, "to_enum", lazy_to_enum, -1);
1946  rb_define_method(rb_cLazy, "enum_for", lazy_to_enum, -1);
1947  rb_define_method(rb_cLazy, "map", lazy_map, 0);
1948  rb_define_method(rb_cLazy, "collect", lazy_map, 0);
1949  rb_define_method(rb_cLazy, "flat_map", lazy_flat_map, 0);
1950  rb_define_method(rb_cLazy, "collect_concat", lazy_flat_map, 0);
1951  rb_define_method(rb_cLazy, "select", lazy_select, 0);
1952  rb_define_method(rb_cLazy, "find_all", lazy_select, 0);
1953  rb_define_method(rb_cLazy, "reject", lazy_reject, 0);
1954  rb_define_method(rb_cLazy, "grep", lazy_grep, 1);
1955  rb_define_method(rb_cLazy, "zip", lazy_zip, -1);
1956  rb_define_method(rb_cLazy, "take", lazy_take, 1);
1957  rb_define_method(rb_cLazy, "take_while", lazy_take_while, 0);
1958  rb_define_method(rb_cLazy, "drop", lazy_drop, 1);
1959  rb_define_method(rb_cLazy, "drop_while", lazy_drop_while, 0);
1960  rb_define_method(rb_cLazy, "lazy", lazy_lazy, 0);
1961  rb_define_method(rb_cLazy, "chunk", lazy_super, -1);
1962  rb_define_method(rb_cLazy, "slice_before", lazy_super, -1);
1963 
1964  rb_define_alias(rb_cLazy, "force", "to_a");
1965 
1966  rb_eStopIteration = rb_define_class("StopIteration", rb_eIndexError);
1968 
1969  /* Generator */
1974  rb_define_method(rb_cGenerator, "initialize_copy", generator_init_copy, 1);
1976 
1977  /* Yielder */
1980  rb_define_method(rb_cYielder, "initialize", yielder_initialize, 0);
1983 
1984  rb_provide("enumerator.so"); /* for backward compatibility */
1985 }
1986 
1987 void
1989 {
1990  id_rewind = rb_intern("rewind");
1991  id_each = rb_intern("each");
1992  id_call = rb_intern("call");
1993  id_size = rb_intern("size");
1994  id_yield = rb_intern("yield");
1995  id_new = rb_intern("new");
1996  id_initialize = rb_intern("initialize");
1997  id_next = rb_intern("next");
1998  id_result = rb_intern("result");
1999  id_lazy = rb_intern("lazy");
2000  id_eqq = rb_intern("===");
2001  id_receiver = rb_intern("receiver");
2002  id_arguments = rb_intern("arguments");
2003  id_memo = rb_intern("memo");
2004  id_method = rb_intern("method");
2005  id_force = rb_intern("force");
2006  id_to_enum = rb_intern("to_enum");
2007  sym_each = ID2SYM(id_each);
2008  sym_cycle = ID2SYM(rb_intern("cycle"));
2009 
2010  InitVM(Enumerator);
2011 }
static VALUE lazy_drop(VALUE obj, VALUE n)
Definition: enumerator.c:1814
#define T_SYMBOL
Definition: ruby.h:502
static VALUE lazy_zip_func(VALUE val, VALUE zip_args, int argc, VALUE *argv)
Definition: enumerator.c:1649
VALUE size
Definition: enumerator.c:122
static void enumerator_mark(void *p)
Definition: enumerator.c:143
VALUE rb_ary_new4(long n, const VALUE *elts)
Definition: array.c:451
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1088
#define RARRAY_LEN(a)
Definition: ruby.h:899
static VALUE next_i(VALUE curr, VALUE obj)
Definition: enumerator.c:578
static VALUE rb_cGenerator
Definition: enumerator.c:126
static VALUE sym_cycle
Definition: enumerator.c:109
static VALUE enumerator_rewind(VALUE obj)
Definition: enumerator.c:860
#define INT2NUM(x)
Definition: ruby.h:1178
static VALUE lazy_drop_while_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1827
static VALUE lazy_reject(VALUE obj)
Definition: enumerator.c:1571
int i
Definition: win32ole.c:784
static VALUE lazy_grep(VALUE obj, VALUE pattern)
Definition: enumerator.c:1607
VALUE rb_yield_values(int n,...)
Definition: vm_eval.c:944
int count
Definition: encoding.c:51
static VALUE call_next(VALUE obj)
Definition: enumerator.c:1617
static void generator_mark(void *p)
Definition: enumerator.c:1098
static VALUE enumerator_peek_values(VALUE obj)
Definition: enumerator.c:736
static VALUE yielder_new(void)
Definition: enumerator.c:1089
static VALUE generator_init_copy(VALUE obj, VALUE orig)
Definition: enumerator.c:1191
static size_t generator_memsize(const void *p)
Definition: enumerator.c:1107
VALUE feedvalue
Definition: enumerator.c:120
#define InitVM(ext)
Definition: ruby.h:1651
#define Qtrue
Definition: ruby.h:434
static VALUE yielder_allocate(VALUE klass)
Definition: enumerator.c:1030
VALUE rb_proc_call_with_block(VALUE, int argc, VALUE *argv, VALUE)
Definition: proc.c:584
#define OBJ_INIT_COPY(obj, orig)
Definition: intern.h:268
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1030
static VALUE lazy_size(VALUE self)
Definition: enumerator.c:1234
static VALUE yielder_init(VALUE obj, VALUE proc)
Definition: enumerator.c:1042
VALUE(* size_fn)(ANYARGS)
Definition: enumerator.c:123
ID rb_frame_this_func(void)
Definition: eval.c:902
VALUE rb_eTypeError
Definition: error.c:516
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:822
#define rb_long2int(n)
Definition: ruby.h:325
static VALUE enumerator_with_object_i(VALUE val, VALUE memo, int argc, VALUE *argv)
Definition: enumerator.c:519
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:2166
union RNode::@93 u1
#define SYM2ID(x)
Definition: ruby.h:364
VALUE rb_fiber_yield(int argc, VALUE *argv)
Definition: cont.c:1394
static const rb_data_type_t generator_data_type
Definition: enumerator.c:1112
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:465
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:773
static VALUE enumerator_init(VALUE enum_obj, VALUE obj, VALUE meth, int argc, VALUE *argv, VALUE(*size_fn)(ANYARGS), VALUE size)
Definition: enumerator.c:269
static VALUE enumerator_next_values(VALUE obj)
Definition: enumerator.c:672
VALUE rb_cEnumerator
Definition: enumerator.c:105
VALUE rb_fiber_alive_p(VALUE fibval)
Definition: cont.c:1422
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:534
VALUE rb_to_int(VALUE)
Definition: object.c:2482
#define Check_Type(v, t)
Definition: ruby.h:539
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1788
VALUE obj
Definition: enumerator.c:114
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1116
VALUE rb_fiber_current(void)
Definition: cont.c:1230
static const rb_data_type_t yielder_data_type
Definition: enumerator.c:1007
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4881
#define RB_GC_GUARD(v)
Definition: ruby.h:530
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE proc
Definition: enumerator.c:129
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:593
static VALUE enum_size(VALUE self)
Definition: enumerator.c:1227
#define generator_free
Definition: enumerator.c:1104
VALUE rb_ary_new3(long n,...)
Definition: array.c:432
static VALUE enumerator_next(VALUE obj)
Definition: enumerator.c:729
static VALUE lazy_to_enum_i(VALUE self, VALUE meth, int argc, VALUE *argv, VALUE(*size_fn)(ANYARGS))
Definition: enumerator.c:1386
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:684
static VALUE enumerator_block_call(VALUE obj, rb_block_call_func *func, VALUE arg)
Definition: enumerator.c:419
void rb_gc_mark(VALUE ptr)
Definition: gc.c:2600
static ID id_lazy
Definition: enumerator.c:108
#define T_ARRAY
Definition: ruby.h:492
static VALUE lazy_to_enum(int argc, VALUE *argv, VALUE self)
Definition: enumerator.c:1416
VALUE rb_block_call(VALUE, ID, int, VALUE *, VALUE(*)(ANYARGS), VALUE)
Definition: vm_eval.c:1120
static VALUE lazy_drop_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1797
#define FIXNUM_P(f)
Definition: ruby.h:355
static VALUE yielder_yield(VALUE obj, VALUE args)
Definition: enumerator.c:1068
static VALUE generator_initialize(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:1164
static VALUE generator_allocate(VALUE klass)
Definition: enumerator.c:1135
static VALUE yielder_yield_i(VALUE obj, VALUE memo, int argc, VALUE *argv)
Definition: enumerator.c:1083
#define OBJ_TAINTED(x)
Definition: ruby.h:1153
static ID id_memo
Definition: enumerator.c:108
const char * rb_obj_classname(VALUE)
Definition: variable.c:396
static VALUE lazy_init_iterator(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1246
static VALUE enumerator_with_object(VALUE obj, VALUE memo)
Definition: enumerator.c:555
void InitVM_Enumerator(void)
Definition: enumerator.c:1916
static VALUE lazy_flat_map(VALUE obj)
Definition: enumerator.c:1525
Definition: node.h:239
Win32OLEIDispatch * p
Definition: win32ole.c:786
VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv)
Definition: enumerator.c:398
void rb_exc_raise(VALUE mesg)
Definition: eval.c:527
static VALUE lazy_map(VALUE obj)
Definition: enumerator.c:1441
static ID id_initialize
Definition: enumerator.c:107
static struct enumerator * enumerator_ptr(VALUE obj)
Definition: enumerator.c:174
int args
Definition: win32ole.c:785
VALUE rb_obj_dup(VALUE)
Definition: object.c:347
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1537
VALUE rb_enumeratorize_with_size(VALUE obj, VALUE meth, int argc, VALUE *argv, VALUE(*size_fn)(ANYARGS))
Definition: enumerator.c:407
static VALUE enumerable_lazy(VALUE obj)
Definition: enumerator.c:1377
void rb_iter_break(void)
Definition: vm.c:1028
static ID id_to_enum
Definition: enumerator.c:107
static VALUE get_next_values(VALUE obj, struct enumerator *e)
Definition: enumerator.c:600
VALUE rb_fiber_resume(VALUE fibval, int argc, VALUE *argv)
Definition: cont.c:1378
static VALUE lazy_flat_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1482
static VALUE enumerator_each(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:442
static VALUE enumerator_with_index(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:489
int rb_block_given_p(void)
Definition: eval.c:672
static VALUE obj_to_enum(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:241
VALUE rb_ary_cat(VALUE ary, const VALUE *ptr, long len)
Definition: array.c:833
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1426
VALUE rb_str_buf_cat2(VALUE, const char *)
Definition: string.c:1961
static ID id_next
Definition: enumerator.c:108
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1414
static VALUE enumerator_peek_values_m(VALUE obj)
Definition: enumerator.c:775
#define NIL_P(v)
Definition: ruby.h:446
static VALUE enumerator_size(VALUE obj)
Definition: enumerator.c:973
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:488
static void next_init(VALUE obj, struct enumerator *e)
Definition: enumerator.c:591
static VALUE lazy_take_size(VALUE generator, VALUE args, VALUE lazy)
Definition: enumerator.c:1733
VALUE value
Definition: node.h:245
VALUE proc
Definition: enumerator.c:133
VALUE rb_fiber_new(VALUE(*func)(ANYARGS), VALUE obj)
Definition: cont.c:1120
#define T_FLOAT
Definition: ruby.h:489
static VALUE stop_result(VALUE self)
Definition: enumerator.c:1910
#define OBJ_UNTRUST(x)
Definition: ruby.h:1156
int argc
Definition: ruby.c:130
#define Qfalse
Definition: ruby.h:433
static VALUE enumerator_with_index_i(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:461
static VALUE lazy_take_while(VALUE obj)
Definition: enumerator.c:1772
static VALUE lazy_select(VALUE obj)
Definition: enumerator.c:1548
VALUE rb_cLazy
Definition: enumerator.c:106
static ID id_result
Definition: enumerator.c:108
static VALUE generator_init(VALUE obj, VALUE proc)
Definition: enumerator.c:1147
VALUE lookahead
Definition: enumerator.c:119
VALUE rb_eIndexError
Definition: error.c:518
static VALUE lazy_receiver_size(VALUE generator, VALUE args, VALUE lazy)
Definition: enumerator.c:1240
void rb_need_block(void)
Definition: eval.c:693
static VALUE yielder_yield_push(VALUE obj, VALUE args)
Definition: enumerator.c:1076
VALUE rb_int_succ(VALUE num)
Definition: numeric.c:2410
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1539
#define enumerator_free
Definition: enumerator.c:156
VALUE rb_yield(VALUE)
Definition: vm_eval.c:933
static VALUE lazy_super(int argc, VALUE *argv, VALUE lazy)
Definition: enumerator.c:1851
static ID id_yield
Definition: enumerator.c:107
#define TRUE
Definition: nkf.h:175
static VALUE lazy_init_block_i(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1272
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:91
VALUE rb_check_funcall(VALUE, ID, int, VALUE *)
Definition: vm_eval.c:408
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:804
#define OBJ_UNTRUSTED(x)
Definition: ruby.h:1155
VALUE rb_mEnumerable
Definition: enum.c:20
static VALUE lazy_drop_size(VALUE generator, VALUE args, VALUE lazy)
Definition: enumerator.c:1783
static VALUE lazy_take(VALUE obj, VALUE n)
Definition: enumerator.c:1743
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1275
#define const
Definition: strftime.c:102
static VALUE enumerator_each_with_index(VALUE obj)
Definition: enumerator.c:513
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1128
unsigned long ID
Definition: ruby.h:105
static VALUE lazy_grep_iter(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1595
VALUE fib
Definition: enumerator.c:117
#define Qnil
Definition: ruby.h:435
VALUE rb_exc_new2(VALUE etype, const char *s)
Definition: error.c:547
VALUE rb_eStopIteration
Definition: enumerator.c:111
static ID id_eqq
Definition: enumerator.c:108
#define OBJ_TAINT(x)
Definition: ruby.h:1154
unsigned long VALUE
Definition: ruby.h:104
static VALUE result
Definition: nkf.c:40
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
Definition: intern.h:215
static size_t enumerator_memsize(const void *p)
Definition: enumerator.c:159
VALUE rb_rescue2(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2,...)
Definition: eval.c:701
VALUE stop_exc
Definition: enumerator.c:121
VALUE rb_call_super(int, const VALUE *)
Definition: vm_eval.c:273
#define INFINITY
Definition: missing.h:138
VALUE rb_proc_call(VALUE, VALUE)
Definition: proc.c:571
static VALUE enumerator_initialize(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:332
VALUE rb_block_call_func(VALUE, VALUE, int, VALUE *)
Definition: ruby.h:1393
#define RARRAY_LENINT(ary)
Definition: ruby.h:908
static VALUE generator_each(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:1212
static VALUE lazy_lazy(VALUE obj)
Definition: enumerator.c:1857
static VALUE lazy_take_while_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1763
static VALUE lazy_initialize(int argc, VALUE *argv, VALUE self)
Definition: enumerator.c:1307
#define LONG2NUM(x)
Definition: ruby.h:1199
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:1598
#define recur(fmt)
#define NEW_MEMO(a, b, c)
Definition: node.h:460
static ID id_receiver
Definition: enumerator.c:108
VALUE rb_enum_values_pack(int argc, VALUE *argv)
Definition: enum.c:33
VALUE dst
Definition: enumerator.c:118
#define RFLOAT_VALUE(v)
Definition: ruby.h:836
int size
Definition: encoding.c:52
VALUE rb_yield_values2(int n, const VALUE *argv)
Definition: vm_eval.c:966
#define rb_check_arity(argc, min, max)
Definition: intern.h:277
#define INT2FIX(i)
Definition: ruby.h:241
#define UNLIMITED_ARGUMENTS
Definition: intern.h:54
static VALUE sym_each
Definition: enumerator.c:109
static VALUE inspect_enumerator(VALUE obj, VALUE dummy, int recur)
Definition: enumerator.c:875
static struct generator * generator_ptr(VALUE obj)
Definition: enumerator.c:1122
VALUE rb_block_proc(void)
Definition: proc.c:458
static VALUE lazy_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1432
#define ANYARGS
Definition: defines.h:57
static VALUE next_stopped(VALUE obj)
Definition: enumerator.c:1623
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:557
#define RARRAY_PTR(a)
Definition: ruby.h:904
static void yielder_mark(void *p)
Definition: enumerator.c:993
#define LONG2FIX(i)
Definition: ruby.h:242
#define RTEST(v)
Definition: ruby.h:445
VALUE rb_proc_new(VALUE(*)(ANYARGS), VALUE)
Definition: proc.c:2018
static ID id_arguments
Definition: enumerator.c:108
v
Definition: win32ole.c:798
static ID id_force
Definition: enumerator.c:108
void Init_Enumerator(void)
Definition: enumerator.c:1988
static ID id_size
Definition: enumerator.c:107
static VALUE next_ii(VALUE i, VALUE obj, int argc, VALUE *argv)
Definition: enumerator.c:564
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1019
VALUE rb_ary_dup(VALUE ary)
Definition: array.c:1766
static VALUE enumerator_allocate(VALUE klass)
Definition: enumerator.c:257
static VALUE lazy_zip_arrays_func(VALUE val, VALUE arrays, int argc, VALUE *argv)
Definition: enumerator.c:1629
static VALUE enumerator_init_copy(VALUE obj, VALUE orig)
Definition: enumerator.c:365
static VALUE lazy_flat_map_to_ary(VALUE obj, VALUE yielder)
Definition: enumerator.c:1466
VALUE rb_ary_new2(long capa)
Definition: array.c:417
static const rb_data_type_t enumerator_data_type
Definition: enumerator.c:164
static ID id_call
Definition: enumerator.c:107
#define ID2SYM(x)
Definition: ruby.h:363
static VALUE yielder_initialize(VALUE obj)
Definition: enumerator.c:1059
const char * rb_id2name(ID id)
Definition: ripper.c:17012
static VALUE ary2sv(VALUE args, int dup)
Definition: enumerator.c:687
VALUE args
Definition: enumerator.c:116
VALUE rb_inspect(VALUE)
Definition: object.c:411
static VALUE lazy_flat_map_i(VALUE i, VALUE yielder, int argc, VALUE *argv)
Definition: enumerator.c:1453
static ID id_new
Definition: enumerator.c:107
static VALUE enumerator_peek(VALUE obj)
Definition: enumerator.c:803
static VALUE lazy_set_method(VALUE lazy, VALUE args, VALUE(*size_fn)(ANYARGS))
Definition: enumerator.c:1329
static size_t yielder_memsize(const void *p)
Definition: enumerator.c:1002
static VALUE lazy_drop_while(VALUE obj)
Definition: enumerator.c:1840
static VALUE enumerator_inspect(VALUE obj)
Definition: enumerator.c:956
static ID id_rewind
Definition: enumerator.c:107
static VALUE lazy_zip(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:1681
#define yielder_free
Definition: enumerator.c:999
#define rb_intern(str)
static VALUE enumerator_feed(VALUE obj, VALUE v)
Definition: enumerator.c:838
#define NULL
Definition: _sdbm.c:102
#define FIX2LONG(x)
Definition: ruby.h:353
#define Qundef
Definition: ruby.h:436
static VALUE lazy_reject_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1560
static VALUE rb_cYielder
Definition: enumerator.c:126
static VALUE lazy_grep_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1583
static struct yielder * yielder_ptr(VALUE obj)
Definition: enumerator.c:1017
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
static ID id_each
Definition: enumerator.c:107
void rb_provide(const char *)
Definition: load.c:566
void rb_warn(const char *fmt,...)
Definition: error.c:221
ID rb_to_id(VALUE)
Definition: string.c:8172
VALUE rb_eArgError
Definition: error.c:517
static ID id_method
Definition: enumerator.c:108
static VALUE lazy_flat_map_each(VALUE obj, VALUE yielder)
Definition: enumerator.c:1459
#define NUM2LONG(x)
Definition: ruby.h:592
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1122
char ** argv
Definition: ruby.c:131
static VALUE lazy_take_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1714
static VALUE lazy_select_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1537