ruby-changes:52483
From: mrkn <ko1@a...>
Date: Wed, 12 Sep 2018 14:35:51 +0900 (JST)
Subject: [ruby-changes:52483] mrkn:r64692 (trunk): enumerator.c: Fix ArithmeticSequence for complex step
mrkn 2018-09-12 14:35:46 +0900 (Wed, 12 Sep 2018) New Revision: 64692 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64692 Log: enumerator.c: Fix ArithmeticSequence for complex step Make sure Enumerator::ArithmeticSequence#each to work well for a complex step value. Modified files: trunk/enumerator.c trunk/test/ruby/test_arithmetic_sequence.rb Index: test/ruby/test_arithmetic_sequence.rb =================================================================== --- test/ruby/test_arithmetic_sequence.rb (revision 64691) +++ test/ruby/test_arithmetic_sequence.rb (revision 64692) @@ -396,4 +396,24 @@ class TestArithmeticSequence < Test::Uni https://github.com/ruby/ruby/blob/trunk/test/ruby/test_arithmetic_sequence.rb#L396 assert_equal(4, (10...2).step(-2).size) assert_equal(Float::INFINITY, (1..).step(-1).size) end + + def assert_num_equal_type(ary1, ary2, message=nil) + assert_equal(ary1.length, ary2.length, message) + ary1.zip(ary2) do |e1, e2| + assert_equal(e1.class, e2.class, message) + if e1.is_a? Complex + assert_equal(e1.real, e2.real, message) + assert_equal(e1.imag, e2.imag, message) + else + assert_equal(e1, e2, message) + end + end + end + + def test_complex + assert_num_equal_type([1, 1+1i, 1+2i], (1..).step(1i).take(3)) + assert_num_equal_type([1, 1+1.0i, 1+2.0i], (1..).step(1.0i).take(3)) + assert_num_equal_type([0.0, 0.0+1.0i, 0.0+2.0i], (0.0..).step(1.0i).take(3)) + assert_num_equal_type([0.0+0.0i, 0.0+1.0i, 0.0+2.0i], (0.0i..).step(1.0i).take(3)) + end end Index: enumerator.c =================================================================== --- enumerator.c (revision 64691) +++ enumerator.c (revision 64692) @@ -2661,7 +2661,8 @@ arith_seq_each(VALUE self) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L2661 s = arith_seq_step(self); x = arith_seq_exclude_end_p(self); - if (ruby_float_step(c, e, s, x, TRUE)) { + if (RB_TYPE_P(c, T_FLOAT) && RB_TYPE_P(s, T_FLOAT) && + ruby_float_step(c, e, s, x, TRUE)) { return self; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/