ruby-changes:27006
From: marcandre <ko1@a...>
Date: Tue, 5 Feb 2013 12:50:11 +0900 (JST)
Subject: [ruby-changes:27006] marcandRe: r39058 (trunk): * enumerator.c: Use to_enum for Enumerable methods returning Enumerators.
marcandre 2013-02-05 12:49:59 +0900 (Tue, 05 Feb 2013) New Revision: 39058 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39058 Log: * enumerator.c: Use to_enum for Enumerable methods returning Enumerators. This makes Lazy#cycle no longer needed, so it was removed. Make Enumerator#chunk and slice_before return lazy Enumerators. [Bug #7715] * internal.h: Remove ref to rb_enum_cycle_size; no longer needed * enum.c: Make enum_cycle_size static. * test/ruby/test_lazy_enumerator.rb: Test for above Modified files: trunk/ChangeLog trunk/enum.c trunk/enumerator.c trunk/internal.h trunk/test/ruby/test_lazy_enumerator.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 39057) +++ ChangeLog (revision 39058) @@ -1,3 +1,17 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Feb 5 12:48:38 2013 Marc-Andre Lafortune <ruby-core@m...> + + * enumerator.c: Use to_enum for Enumerable methods returning + Enumerators. + This makes Lazy#cycle no longer needed, so it was removed. + Make Enumerator#chunk and slice_before return lazy Enumerators. + [Bug #7715] + + * internal.h: Remove ref to rb_enum_cycle_size; no longer needed + + * enum.c: Make enum_cycle_size static. + + * test/ruby/test_lazy_enumerator.rb: Test for above + Tue Feb 5 12:48:10 2013 Marc-Andre Lafortune <ruby-core@m...> * enumerator.c: Finalize and document Lazy.new. [Bug #7248] Index: enumerator.c =================================================================== --- enumerator.c (revision 39057) +++ enumerator.c (revision 39058) @@ -463,7 +463,13 @@ lazy_to_enum_i(VALUE self, VALUE meth, i https://github.com/ruby/ruby/blob/trunk/enumerator.c#L463 VALUE rb_enumeratorize_with_size(VALUE obj, VALUE meth, int argc, VALUE *argv, VALUE (*size_fn)(ANYARGS)) { - return enumerator_init(enumerator_allocate(rb_cEnumerator), obj, meth, argc, argv, size_fn, Qnil); + /* Similar effect as calling obj.to_enum, i.e. dispatching to either + Kernel#to_enum vs Lazy#to_enum */ + if (RTEST(rb_obj_is_kind_of(obj, rb_cLazy))) + return lazy_to_enum_i(obj, meth, argc, argv, size_fn); + else + return enumerator_init(enumerator_allocate(rb_cEnumerator), + obj, meth, argc, argv, size_fn, Qnil); } static VALUE @@ -1893,12 +1899,9 @@ lazy_drop_while(VALUE obj) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L1899 } static VALUE -lazy_cycle(int argc, VALUE *argv, VALUE obj) +lazy_super(int argc, VALUE *argv, VALUE lazy) { - if (rb_block_given_p()) { - return rb_call_super(argc, argv); - } - return lazy_to_enum_i(obj, sym_cycle, argc, argv, rb_enum_cycle_size); + return enumerable_lazy(rb_call_super(argc, argv)); } static VALUE @@ -2005,8 +2008,9 @@ InitVM_Enumerator(void) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L2008 rb_define_method(rb_cLazy, "take_while", lazy_take_while, 0); rb_define_method(rb_cLazy, "drop", lazy_drop, 1); rb_define_method(rb_cLazy, "drop_while", lazy_drop_while, 0); - rb_define_method(rb_cLazy, "cycle", lazy_cycle, -1); rb_define_method(rb_cLazy, "lazy", lazy_lazy, 0); + rb_define_method(rb_cLazy, "chunk", lazy_super, -1); + rb_define_method(rb_cLazy, "slice_before", lazy_super, -1); rb_define_alias(rb_cLazy, "force", "to_a"); Index: enum.c =================================================================== --- enum.c (revision 39057) +++ enum.c (revision 39058) @@ -2238,9 +2238,8 @@ cycle_i(VALUE i, VALUE ary, int argc, VA https://github.com/ruby/ruby/blob/trunk/enum.c#L2238 return Qnil; } -#define enum_cycle_size rb_enum_cycle_size -VALUE -rb_enum_cycle_size(VALUE self, VALUE args) +static VALUE +enum_cycle_size(VALUE self, VALUE args) { long mul; VALUE n = Qnil; Index: internal.h =================================================================== --- internal.h (revision 39057) +++ internal.h (revision 39058) @@ -90,9 +90,6 @@ ID rb_id_encoding(void); https://github.com/ruby/ruby/blob/trunk/internal.h#L90 /* encoding.c */ void rb_gc_mark_encodings(void); -/* enum.c */ -VALUE rb_enum_cycle_size(VALUE self, VALUE args); - /* error.c */ NORETURN(PRINTF_ARGS(void rb_compile_bug(const char*, int, const char*, ...), 3, 4)); VALUE rb_check_backtrace(VALUE); Index: test/ruby/test_lazy_enumerator.rb =================================================================== --- test/ruby/test_lazy_enumerator.rb (revision 39057) +++ test/ruby/test_lazy_enumerator.rb (revision 39058) @@ -447,4 +447,20 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/ruby/test_lazy_enumerator.rb#L447 assert_raise(ArgumentError){ [].lazy.send(method) } end end + + def test_laziness_conservation + bug7507 = '[ruby-core:51510]' + { + slice_before: //, + with_index: nil, + cycle: nil, + each_with_object: 42, + each_slice: 42, + each_entry: nil, + each_cons: 42, + }.each do |method, arg| + assert_equal Enumerator::Lazy, [].lazy.send(method, *arg).class, bug7507 + end + assert_equal Enumerator::Lazy, [].lazy.chunk{}.class, bug7507 + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/