/* * call-seq: * e.with_object(obj) {|(*args), memo_obj| ... } * e.with_object(obj) * * Iterates the given block for each element with an arbitrary * object given, and returns the initially given object. * * If no block is given, returns an enumerator. * */ static VALUE enumerator_with_object(VALUE obj, SEL sel, VALUE memo) { RETURN_ENUMERATOR(obj, 1, &memo); enumerator_block_call(obj, enumerator_with_object_i, memo); return memo; }
/* * call-seq: * e.each_with_object(obj) {|(*args), obj| ... } * e.each_with_object(obj) * e.with_object(obj) {|(*args), obj| ... } * e.with_object(obj) * * Iterates the given block for each element with an arbitrary object, +obj+, * and returns +obj+ * * If no block is given, returns a new Enumerator. * * === Example * * to_three = Enumerator.new do |y| * 3.times do |x| * y << x * end * end * * to_three_with_string = to_three.with_object("foo") * to_three_with_string.each do |x,string| * puts "#{string}: #{x}" * end * * # => foo:0 * # => foo:1 * # => foo:2 */ static VALUE enumerator_with_object(VALUE obj, VALUE memo) { RETURN_SIZED_ENUMERATOR(obj, 1, &memo, enumerator_enum_size); enumerator_block_call(obj, enumerator_with_object_i, memo); return memo; }
/* * call-seq: * enum.each {...} * * Iterates the given block using the object and the method specified * in the first place. If no block is given, returns self. * */ static VALUE enumerator_each(VALUE obj, SEL sel) { if (!rb_block_given_p()) { return obj; } return enumerator_block_call(obj, enumerator_each_i, obj); }
/* * call-seq: * e.with_index(offset = 0) {|(*args), idx| ... } * e.with_index(offset = 0) * * Iterates the given block for each element with an index, which * starts from +offset+. If no block is given, returns a new Enumerator * that includes the index, starting from +offset+ * * +offset+:: the starting index to use * */ static VALUE enumerator_with_index(int argc, VALUE *argv, VALUE obj) { VALUE memo; rb_scan_args(argc, argv, "01", &memo); RETURN_ENUMERATOR(obj, argc, argv); memo = NIL_P(memo) ? 0 : (VALUE)NUM2LONG(memo); return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)&memo); }
/* * call-seq: * e.with_index(offset = 0) {|(*args), idx| ... } * e.with_index(offset = 0) * * Iterates the given block for each element with an index, which * starts from +offset+. If no block is given, returns a new Enumerator * that includes the index, starting from +offset+ * * +offset+:: the starting index to use * */ static VALUE enumerator_with_index(int argc, VALUE *argv, VALUE obj) { VALUE memo; rb_scan_args(argc, argv, "01", &memo); RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_enum_size); if (NIL_P(memo)) memo = INT2FIX(0); else memo = rb_to_int(memo); return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)&memo); }
/* * call-seq: * enum.each {...} * * Iterates over the block according to how this Enumerable was constructed. * If no block is given, returns self. * */ static VALUE enumerator_each(int argc, VALUE *argv, VALUE obj) { if (argc > 0) { struct enumerator *e = enumerator_ptr(obj = rb_obj_dup(obj)); VALUE args = e->args; if (args) { args = rb_ary_dup(args); rb_ary_cat(args, argv, argc); } else { args = rb_ary_new4(argc, argv); } e->args = args; } if (!rb_block_given_p()) return obj; return enumerator_block_call(obj, 0, obj); }
/* * call-seq: * enum.each {...} * * Iterates over the block according to how this Enumerable was constructed. * If no block is given, returns self. * */ static VALUE enumerator_each(VALUE obj) { if (!rb_block_given_p()) return obj; return enumerator_block_call(obj, 0, obj); }