1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67435 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2019-04-04 03:34:55 +00:00
parent 53d3fe0643
commit f1fb989f1a
2 changed files with 13 additions and 0 deletions

View file

@ -3029,6 +3029,9 @@ arith_seq_first(int argc, VALUE *argv, VALUE self)
e = arith_seq_end(self); e = arith_seq_end(self);
s = arith_seq_step(self); s = arith_seq_step(self);
if (argc == 0) { if (argc == 0) {
if (NIL_P(b)) {
return Qnil;
}
if (!NIL_P(e)) { if (!NIL_P(e)) {
VALUE zero = INT2FIX(0); VALUE zero = INT2FIX(0);
int r = rb_cmpint(rb_num_coerce_cmp(s, zero, idCmp), s, zero); int r = rb_cmpint(rb_num_coerce_cmp(s, zero, idCmp), s, zero);

View file

@ -170,6 +170,16 @@ class TestArithmeticSequence < Test::Unit::TestCase
assert_equal(10.0, seq.first) assert_equal(10.0, seq.first)
assert_equal([10.0], seq.first(1)) assert_equal([10.0], seq.first(1))
assert_equal([10.0, 8.0, 6.0], seq.first(3)) 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 end
def test_first_bug15518 def test_first_bug15518