Delete first x entires of an array

ruby, ruby-on-rails

Solution

I have an array of items, and I need to delete the first x items of it.

To non-destructive deletion

`Array#drop(x)` will do the work for you.

Drops first n elements from ary and returns the rest of the elements in an array.If a negative number is given, raises an ArgumentError.

my_items = [ 'item1', 'item2', 'item3', 'item4' ]
p my_items.drop(2)
p my_items

# >>["item3", "item4"]
# >>["item1", "item2", "item3", "item4"]

To destructive deletion

`Array#shift`

Removes the first element of self and returns it (shifting all other elements down by one). Returns nil if the array is empty.If a number n is given, returns an array of the first n elements (or less) just like array.slice!(0, n) does.

my_items = [ 'item1', 'item2', 'item3', 'item4' ]
my_items.shift(2)
p my_items # => ["item3", "item4"]

Problem

I have an array of items, and I need to delete the first x items of it. Is there a built-in function in the Ruby Array class to do this? I had a search around and only found, what looked like, incredibly messy or inefficient ways to do it. I'd preferably like something like this: ``` my_items = [ 'item1', 'item2', 'item3', 'item4' ] trimmed_items = my_items.delete(y, x) # deleting x entries from index y ```

Original source