diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-07-06 00:04:56 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-07-06 00:04:56 +0000 |
commit | def66ceca3ec7f899ab78bb21f534a6172b4c8a1 (patch) | |
tree | 7eb015702ddc11782652806d83263edd325e0b17 /enum.c | |
parent | e86d1ce77e43df78decae079eef94fc9099cd9f1 (diff) |
* enum.c (enum_join): deals with self recursive objects to get rid
of infinite recursion. [ruby-core:24150]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23966 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r-- | enum.c | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -1803,6 +1803,30 @@ enum_cycle(int argc, VALUE *argv, VALUE obj) return Qnil; /* not reached */ } +static VALUE +join_i(VALUE i, VALUE args, int argc, VALUE *argv) +{ + VALUE *arg = (VALUE *)args; + ENUM_WANT_SVALUE(); + if (!arg[0]) { + arg[0] = rb_usascii_str_new(0, 0); + } + else if (!NIL_P(arg[1])) { + rb_str_buf_append(arg[0], arg[1]); + } + return rb_str_buf_append(arg[0], rb_obj_as_string(i)); +} + +VALUE +rb_enum_join(VALUE obj, VALUE sep) +{ + VALUE args[2]; + args[0] = 0; + args[1] = sep; + rb_block_call(obj, id_each, 0, 0, join_i, (VALUE)args); + return args[0] ? args[0] : rb_str_new(0, 0); +} + /* * call-seq: * enum.join(sep=$,) -> str @@ -1819,7 +1843,7 @@ enum_join(int argc, VALUE *argv, VALUE obj) rb_scan_args(argc, argv, "01", &sep); if (NIL_P(sep)) sep = rb_output_fs; - return rb_ary_join(enum_to_a(0, 0, obj), sep); + return rb_enum_join(obj, sep); } /* |