Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pop/shift a ruby array multiple times

Tags:

arrays

ruby

My code currently looks like this

numbers = [1, 2, 3, 4, 5]

def pop_three
  pop = []
  3.times { pop << numbers.pop }
  return pop
end

Is there any way to do what's inside the pop_three method in one line?

I basically want to do something like numbers.slice(0, 3) but deleting the array items that are in the slice.

Uhm...hrmmm, I think I just realized I can try slice!

like image 236
Brand Avatar asked Sep 12 '11 15:09

Brand


1 Answers

Yes

numbers.pop(3)

Or

numbers.shift(3)

If you want this other side.

like image 193
mb14 Avatar answered Sep 22 '22 01:09

mb14