summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormanga_osyo <manga.osyo@gmail.com>2019-04-30 23:18:44 +0900
committerYusuke Endoh <mame@ruby-lang.org>2019-05-01 00:03:30 +0900
commit4e88e8692844a2a317bc19481f0f2601b6f00955 (patch)
tree07e66ab1a53b2f881475dcfa9659c0b61d642658
parent0eedec68673fa74960dec80b26659263ec3b6a9a (diff)
Add exception support in `Range#first`.
Closes: https://github.com/ruby/ruby/pull/2163
-rw-r--r--range.c3
-rw-r--r--test/ruby/test_range.rb2
2 files changed, 5 insertions, 0 deletions
diff --git a/range.c b/range.c
index 4180a663aa..03ca38d611 100644
--- a/range.c
+++ b/range.c
@@ -1012,6 +1012,9 @@ range_first(int argc, VALUE *argv, VALUE range)
{
VALUE n, ary[2];
+ if (NIL_P(RANGE_BEG(range))) {
+ rb_raise(rb_eRangeError, "cannot get the first element of beginless range");
+ }
if (argc == 0) return RANGE_BEG(range);
rb_scan_args(argc, argv, "1", &n);
diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb
index b1008d041f..5d21d30257 100644
--- a/test/ruby/test_range.rb
+++ b/test/ruby/test_range.rb
@@ -443,6 +443,8 @@ class TestRange < Test::Unit::TestCase
assert_equal("a", ("a"..nil).first)
assert_raise(RangeError) { (0..nil).last }
assert_raise(RangeError) { (0..nil).last(3) }
+ assert_raise(RangeError) { (nil..0).first }
+ assert_raise(RangeError) { (nil..0).first(3) }
assert_equal([0, 1, 2], (0..10).first(3.0))
assert_equal([8, 9, 10], (0..10).last(3.0))