开发者

How do you do an in-place drop in Ruby

Can anyb开发者_开发技巧ody explain why Array in Ruby doesn't have a drop! method?

Is there a way to drop or slice an array in place?


There's slice!. It can take an index (just like drop, so slice!(index) is the in-place drop you're looking for), a range, or two parameters for start and end.


You can use slice! with a range to accomplish this:

a = [1, 2, 3, 4]
a.drop(2)
=> [3, 4]
a
=> [1, 2, 3, 4]

vs

a = [1, 2, 3, 4]
a.slice!(0..1)
=> [1, 2]
a
=> [3, 4]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜