summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornagachika <nagachika@ruby-lang.org>2022-10-23 19:11:45 +0900
committernagachika <nagachika@ruby-lang.org>2022-10-23 19:11:45 +0900
commit941c888b041decb45034572d766120f9be34986e (patch)
treee6657b0e005767a1ea77a85b69385eb84aa0bbf4
parent5e25ba5d07d42f02485235e3962a4a28373c50e1 (diff)
merge revision(s) bbe5ec78463f8d6ef2e1a3571f17357a3d9ec8e4: [Backport #18994]
rb_int_range_last: properly handle non-exclusive range [Bug #18994] --- range.c | 8 ++++---- spec/ruby/core/range/last_spec.rb | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-)
-rw-r--r--range.c8
-rw-r--r--spec/ruby/core/range/last_spec.rb6
-rw-r--r--version.h2
3 files changed, 11 insertions, 5 deletions
diff --git a/range.c b/range.c
index 6eb7842313..faf6ab997b 100644
--- a/range.c
+++ b/range.c
@@ -1107,10 +1107,6 @@ rb_int_range_last(int argc, VALUE *argv, VALUE range)
x = EXCL(range);
len_1 = rb_int_minus(e, b);
- if (FIXNUM_ZERO_P(len_1) || rb_num_negative_p(len_1)) {
- return rb_ary_new_capa(0);
- }
-
if (x) {
e = rb_int_minus(e, ONE);
len = len_1;
@@ -1119,6 +1115,10 @@ rb_int_range_last(int argc, VALUE *argv, VALUE range)
len = rb_int_plus(len_1, ONE);
}
+ if (FIXNUM_ZERO_P(len) || rb_num_negative_p(len)) {
+ return rb_ary_new_capa(0);
+ }
+
rb_scan_args(argc, argv, "1", &nv);
n = NUM2LONG(nv);
if (n < 0) {
diff --git a/spec/ruby/core/range/last_spec.rb b/spec/ruby/core/range/last_spec.rb
index d7ef776b42..6698686dd5 100644
--- a/spec/ruby/core/range/last_spec.rb
+++ b/spec/ruby/core/range/last_spec.rb
@@ -8,6 +8,12 @@ describe "Range#last" do
(1..5).last(3).should == [3, 4, 5]
end
+ ruby_bug '#18994', '2.7'...'3.2' do
+ it "returns the specified number if elements for single element inclusive range" do
+ (1..1).last(1).should == [1]
+ end
+ end
+
it "returns an empty array for an empty Range" do
(0...0).last(2).should == []
end
diff --git a/version.h b/version.h
index b35f82ffb0..dc042bbf9d 100644
--- a/version.h
+++ b/version.h
@@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 3
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 168
+#define RUBY_PATCHLEVEL 169
#define RUBY_RELEASE_YEAR 2022
#define RUBY_RELEASE_MONTH 10