ruby-changes:12884
From: akr <ko1@a...>
Date: Sat, 22 Aug 2009 00:51:52 +0900 (JST)
Subject: [ruby-changes:12884] Ruby:r24619 (trunk): * enumerator.c (ary2sv): add dup argument.
akr 2009-08-22 00:51:35 +0900 (Sat, 22 Aug 2009) New Revision: 24619 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24619 Log: * 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. Modified files: trunk/ChangeLog trunk/enumerator.c trunk/test/ruby/test_enumerator.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 24618) +++ ChangeLog (revision 24619) @@ -1,3 +1,12 @@ +Sat Aug 22 00:48:08 2009 Tanaka Akira <akr@f...> + + * 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. + Sat Aug 22 00:03:19 2009 Yusuke Endoh <mame@t...> * thread.c (rb_check_deadlock): decrease number of sleepers before Index: enumerator.c =================================================================== --- enumerator.c (revision 24618) +++ enumerator.c (revision 24619) @@ -680,7 +680,7 @@ } static VALUE -ary2sv(VALUE args) +ary2sv(VALUE args, int dup) { if (TYPE(args) != T_ARRAY) return args; @@ -693,6 +693,8 @@ return RARRAY_PTR(args)[0]; default: + if (dup) + return rb_ary_dup(args); return args; } } @@ -715,9 +717,20 @@ 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; +} + /* * call-seq: * e.peek_values => array @@ -744,14 +757,9 @@ */ 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 @@ enumerator_peek(VALUE obj) { VALUE vs = enumerator_peek_values(obj); - return ary2sv(vs); + return ary2sv(vs, 1); } /* @@ -1153,7 +1161,7 @@ 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); Index: test/ruby/test_enumerator.rb =================================================================== --- test/ruby/test_enumerator.rb (revision 24618) +++ test/ruby/test_enumerator.rb (revision 24619) @@ -141,6 +141,28 @@ assert_raise(StopIteration) { e.peek } end + def test_peek_modify + o = Object.new + def o.each + yield 1,2 + end + e = o.to_enum + a = e.peek + a << 3 + assert_equal([1,2], e.peek) + end + + def test_peek_values_modify + o = Object.new + def o.each + yield 1,2 + end + e = o.to_enum + a = e.peek_values + a << 3 + assert_equal([1,2], e.peek) + end + def test_next_after_stopiteration a = [1] e = a.each -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/