In C# you can do:
var list = new List<int>(){1,2,3,4,5};
list.skip(2).take(2); // returns (3,4)
I'm trying to learn all the Ruby enumerable methods, but I don't see an equivalent for skip(n)
a = [1,2,3,4,5]
a.skip(2).take(2) # take exists, skip doesn't
So, what's the "best" Ruby way to do it?
all of these work, but they're pretty ugly.
a.last(a.length - 2).take(2)
(a - a.first(2)).take(2)
a[2...a.length].take(2)
Use drop
. From the docs:
a = [1, 2, 3, 4, 5, 0]
a.drop(3) #=> [4, 5, 0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With