Skip to content

Commit 69ce3a0

Browse files
itaratoeregon
authored andcommitted
Fix array slice with arithmetic sequence for Ruby v 3.2.
1 parent 643aa8d commit 69ce3a0

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/main/ruby/truffleruby/core/array.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ def ==(other)
165165
# negative indexes refer to the end of array
166166
start += len if start < 0
167167
stop += len if stop < 0
168-
169-
stop += 1 unless seq.exclude_end?
170-
diff = stop - start
168+
diff = stop - start + (seq.exclude_end? ? 0 : 1)
171169

172170
is_out_of_bound = start < 0 || start > len
173171

@@ -184,12 +182,15 @@ def ==(other)
184182
return self[start, diff] if step == 1 # step == 1 is a simple slice
185183

186184
# optimize when no step will be done and only start element is returned
187-
return self[start, 1] if (step > 0 && step > diff) || (step < 0 && step < -diff)
185+
return self[start, 1] if (step > 0 && step > diff)
186+
return self[stop, 1] if (step < 0 && step < -diff)
188187

189188
ustep = step.abs
190189
nlen = (diff + ustep - 1) / ustep
191190
i = 0
192-
j = start + (step > 0 ? 0 : diff - 1) # because we inverted negative step ranges
191+
j = start
192+
j += diff - (seq.exclude_end? ? 0 : 1) if step < 0 # because we inverted negative step ranges
193+
193194
res = Array.new(nlen)
194195

195196
while i < nlen

0 commit comments

Comments
 (0)