summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--enumerator.c3
-rw-r--r--test/ruby/test_arithmetic_sequence.rb10
2 files changed, 13 insertions, 0 deletions
diff --git a/enumerator.c b/enumerator.c
index 37f8e6b..cfc6a5d 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -3029,6 +3029,9 @@ arith_seq_first(int argc, VALUE *argv, VALUE self)
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);
diff --git a/test/ruby/test_arithmetic_sequence.rb b/test/ruby/test_arithmetic_sequence.rb
index c977980..5817631 100644
--- a/test/ruby/test_arithmetic_sequence.rb
+++ b/test/ruby/test_arithmetic_sequence.rb
@@ -170,6 +170,16 @@ class TestArithmeticSequence < Test::Unit::TestCase
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