summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-11-03 11:04:35 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-11-03 11:04:35 +0000
commitaae36756dcc4d5debcaabd03379796bc41360bc4 (patch)
treeff4ed4df6473229d846ff4f658094fcf66c4c744 /enum.c
parentc960141333a9bc384bafbcfaa49672e48677d6dd (diff)
downloadruby-aae36756dcc4d5debcaabd03379796bc41360bc4.tar.gz
* object.c (Init_Object): added Object#object_id, new name for
Object#id. [new] * object.c (rb_obj_id_obsolete): give warning for Object#id. * numeric.c (fix_intern): added Fixnum#to_sym. [new] * object.c (sym_to_sym): rename from Symbol#intern * enum.c (enum_zip): added Enumerable#zip. [new] * array.c (rb_ary_zip): added Array#zip. * error.c (init_syserr): remove sys_nerr dependency. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3020 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/enum.c b/enum.c
index c8831fb7b9..c5191fdfba 100644
--- a/enum.c
+++ b/enum.c
@@ -486,6 +486,63 @@ enum_each_with_index(obj)
return obj;
}
+static VALUE
+zip_i(val, memo)
+ VALUE val;
+ NODE *memo;
+{
+ VALUE ary = memo->u1.value;
+ int i = memo->u3.cnt++;
+ int elen = memo->u2.argc+1;
+ VALUE tmp;
+
+ if (i < RARRAY(ary)->len) {
+ tmp = RARRAY(ary)->ptr[i];
+ RARRAY(tmp)->ptr[0] = val;
+ }
+ else {
+ tmp = rb_ary_new2(elen);
+ RARRAY(tmp)->ptr[0] = val;
+ for (i=1; i<elen; i++) {
+ RARRAY(tmp)->ptr[i] = Qnil;
+ }
+ RARRAY(tmp)->len = elen;
+ rb_ary_push(ary, tmp);
+ }
+ return Qnil;
+}
+
+static VALUE
+enum_zip(argc, argv, obj)
+ int argc;
+ VALUE *argv;
+ VALUE obj;
+{
+ int i, j, len;
+ VALUE result;
+ NODE *memo;
+
+ len = 0;
+ for (i=0; i<argc; i++) {
+ argv[i] = rb_convert_type(argv[i], T_ARRAY, "Array", "to_ary");
+ if (RARRAY(argv[i])->len > len) len = RARRAY(argv[i])->len;
+ }
+ result = rb_ary_new2(len);
+ for (i=0; i<len; i++) {
+ VALUE tmp = rb_ary_new2(argc+1);
+
+ rb_ary_push(tmp, Qnil);
+ for (j=0; j<argc; j++) {
+ rb_ary_push(tmp, rb_ary_entry(argv[j], i));
+ }
+ rb_ary_push(result, tmp);
+ }
+ memo = rb_node_newnode(NODE_MEMO, result, argc, 0);
+ rb_iterate(rb_each, obj, zip_i, (VALUE)memo);
+
+ return result;
+}
+
void
Init_Enumerable()
{
@@ -513,6 +570,7 @@ Init_Enumerable()
rb_define_method(rb_mEnumerable,"member?", enum_member, 1);
rb_define_method(rb_mEnumerable,"include?", enum_member, 1);
rb_define_method(rb_mEnumerable,"each_with_index", enum_each_with_index, 0);
+ rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
id_eqq = rb_intern("===");
id_each = rb_intern("each");