Pop / shift a ruby ​​array several times

Currently my code is as follows

numbers = [1, 2, 3, 4, 5] def pop_three pop = [] 3.times { pop << numbers.pop } return pop end 

Is there a way to do what's inside the pop_three method on one line?

Basically I want to do something like numbers.slice(0, 3) , but delete the elements of the array that are in the slice.

Uh ... hmmmm, I think I just realized that I could try a slice!

+4
source share
2 answers

Yes

 numbers.pop(3) 

or

 numbers.shift(3) 

If you want this other side.

+8
source

how about numbers.slice(x.size-3,3).reverse

0
source

All Articles