ruby-changes:55228
From: mrkn <ko1@a...>
Date: Thu, 4 Apr 2019 12:35:00 +0900 (JST)
Subject: [ruby-changes:55228] mrkn:r67435 (trunk): enumerator.c: make arith_seq_first support nil begin
mrkn 2019-04-04 12:34:55 +0900 (Thu, 04 Apr 2019) New Revision: 67435 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=67435 Log: enumerator.c: make arith_seq_first support nil begin * enumerator.c: (arith_seq_first): support nil begin. * test/ruby/test_arithmetic_sequence.rb (test_first): add assertions for beginless and endless arithmetic sequences. Modified files: trunk/enumerator.c trunk/test/ruby/test_arithmetic_sequence.rb Index: enumerator.c =================================================================== --- enumerator.c (revision 67434) +++ enumerator.c (revision 67435) @@ -3029,6 +3029,9 @@ arith_seq_first(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/enumerator.c#L3029 e = arith_seq_end(self); s = arith_seq_step(self); if (argc == 0) { + if (NIL_P(b)) { + return Qnil; + } if (!NIL_P(e)) { VALUE zero = INT2FIX(0); int r = rb_cmpint(rb_num_coerce_cmp(s, zero, idCmp), s, zero); Index: test/ruby/test_arithmetic_sequence.rb =================================================================== --- test/ruby/test_arithmetic_sequence.rb (revision 67434) +++ test/ruby/test_arithmetic_sequence.rb (revision 67435) @@ -170,6 +170,16 @@ class TestArithmeticSequence < Test::Uni https://github.com/ruby/ruby/blob/trunk/test/ruby/test_arithmetic_sequence.rb#L170 assert_equal(10.0, seq.first) assert_equal([10.0], seq.first(1)) assert_equal([10.0, 8.0, 6.0], seq.first(3)) + + seq = (1..).step(2) + assert_equal(1, seq.first) + assert_equal([1], seq.first(1)) + assert_equal([1, 3, 5], seq.first(3)) + + seq = (..10).step(2) + assert_equal(nil, seq.first) + assert_raise(TypeError) { seq.first(1) } + assert_raise(TypeError) { seq.first(3) } end def test_first_bug15518 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/