Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

why does the the Array.slice method work this way?

I am using Ruby2.7 and trying to slice a an Array with the method Array.slice which I thought sliced an Array with given arguments of indices.

what I have tried:

nums = Array((0..100)).slice(4, 9)
puts nums

the output is:

[4, 5, 6, 7, 8, 9, 10, 11, 12]

I expected it to output:

[4, 5, 6, 7, 8]

Why does Array.slice work this way and how do I actually slice an Array with indices?

I can do it easily in python like:

list[index1:index2]

question from:https://stackoverflow.com/questions/65649430/why-does-the-the-array-slice-method-work-this-way

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...