summaryrefslogtreecommitdiff
path: root/enumerator.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-08-21 15:51:35 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-08-21 15:51:35 +0000
commit461bd62fec54d1026ef2496fbc9b9fd676f4d9d9 (patch)
tree6bf4c687fefbf5e371d535f8284adb1906153681 /enumerator.c
parent7609494ffdd74aad9fe9aa4ce6da55e8e50300fb (diff)
downloadruby-461bd62fec54d1026ef2496fbc9b9fd676f4d9d9.tar.gz
* enumerator.c (ary2sv): add dup argument.
(enumerator_next): call ary2sv with dup=0. (enumerator_peek): call ary2sv with dup=1 to return duplicated array. (enumerator_peek_values_m): new function to return duplicated array. (Init_Enumerator): use enumerator_peek_values_m as Enumerator#peek_value. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24619 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enumerator.c')
-rw-r--r--enumerator.c30
1 files changed, 19 insertions, 11 deletions
diff --git a/enumerator.c b/enumerator.c
index 2f9a5589e7..9d5c39009a 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -680,7 +680,7 @@ enumerator_next_values(VALUE obj)
}
static VALUE
-ary2sv(VALUE args)
+ary2sv(VALUE args, int dup)
{
if (TYPE(args) != T_ARRAY)
return args;
@@ -693,6 +693,8 @@ ary2sv(VALUE args)
return RARRAY_PTR(args)[0];
default:
+ if (dup)
+ return rb_ary_dup(args);
return args;
}
}
@@ -715,7 +717,18 @@ static VALUE
enumerator_next(VALUE obj)
{
VALUE vs = enumerator_next_values(obj);
- return ary2sv(vs);
+ return ary2sv(vs, 0);
+}
+
+static VALUE
+enumerator_peek_values(VALUE obj)
+{
+ struct enumerator *e = enumerator_ptr(obj);
+
+ if (e->lookahead == Qundef) {
+ e->lookahead = get_next_values(obj, e);
+ }
+ return e->lookahead;
}
/*
@@ -744,14 +757,9 @@ enumerator_next(VALUE obj)
*/
static VALUE
-enumerator_peek_values(VALUE obj)
+enumerator_peek_values_m(VALUE obj)
{
- struct enumerator *e = enumerator_ptr(obj);
-
- if (e->lookahead == Qundef) {
- e->lookahead = get_next_values(obj, e);
- }
- return e->lookahead;
+ return rb_ary_dup(enumerator_peek_values(obj));
}
/*
@@ -768,7 +776,7 @@ static VALUE
enumerator_peek(VALUE obj)
{
VALUE vs = enumerator_peek_values(obj);
- return ary2sv(vs);
+ return ary2sv(vs, 1);
}
/*
@@ -1153,7 +1161,7 @@ Init_Enumerator(void)
rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, -1);
rb_define_method(rb_cEnumerator, "with_object", enumerator_with_object, 1);
rb_define_method(rb_cEnumerator, "next_values", enumerator_next_values, 0);
- rb_define_method(rb_cEnumerator, "peek_values", enumerator_peek_values, 0);
+ rb_define_method(rb_cEnumerator, "peek_values", enumerator_peek_values_m, 0);
rb_define_method(rb_cEnumerator, "next", enumerator_next, 0);
rb_define_method(rb_cEnumerator, "peek", enumerator_peek, 0);
rb_define_method(rb_cEnumerator, "feed", enumerator_feed, 1);