Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Ruby have Skip(n) like C#?

Tags:

c#

list

ruby

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)
like image 589
jb. Avatar asked Jan 08 '13 07:01

jb.


1 Answers

Use drop. From the docs:

a = [1, 2, 3, 4, 5, 0]
a.drop(3)             #=> [4, 5, 0]
like image 79
Mike Zboray Avatar answered Sep 28 '22 10:09

Mike Zboray