Stack
- Stack is like stacking plates in your Kitchen.
- Last in, first out: LIFO
- In Ruby, you can treat an array as stack because there is
pushandpopmethods. - But what about
peek? Java has peek, what about Ruby? - You can use
.lastinstead of peek. And it will return the item on the top. .lengthor.sizewill return the size of the Stack.
push#
- There are many ways to push items on a stack in Ruby:
pop#
- Removes an item from the end, and returns that item
delete item from the front#
- Shift deletes the item from the front and returns that item.
- Useful when Array is used as a Queue.
last ( instead of peek )#
size/length/count#
- Size, count, length returns the same result.
- Check the article on size, count and length to learn more about their differences.