Class: CollectionUtils::Stack
- Inherits:
-
Object
- Object
- CollectionUtils::Stack
- Defined in:
- lib/collection_utils/stack.rb
Instance Method Summary collapse
-
#initialize(array = []) ⇒ Stack
constructor
Constructors.
-
#is_empty? ⇒ Boolean
Stack’s emptiness.
-
#peek ⇒ Object
View the top element of the stack without removing it.
-
#pop ⇒ Object
Pop will remove the top element from the stack and return the removed element.
-
#push(element) ⇒ Object
Add the element to the stack using push.
-
#size ⇒ Integer
Size of stack.
Constructor Details
#initialize(array = []) ⇒ Stack
Constructors
8 9 10 11 12 13 |
# File 'lib/collection_utils/stack.rb', line 8 def initialize(array=[]) @stack = [] array.each do |element| @stack << element end end |
Instance Method Details
#is_empty? ⇒ Boolean
Returns stack’s emptiness.
42 43 44 |
# File 'lib/collection_utils/stack.rb', line 42 def is_empty? return @stack.size == 0 end |
#peek ⇒ Object
View the top element of the stack without removing it
53 54 55 |
# File 'lib/collection_utils/stack.rb', line 53 def peek return @stack.last end |
#pop ⇒ Object
Pop will remove the top element from the stack and return the removed element.
22 23 24 |
# File 'lib/collection_utils/stack.rb', line 22 def pop return @stack.pop end |
#push(element) ⇒ Object
Add the element to the stack using push
34 35 36 |
# File 'lib/collection_utils/stack.rb', line 34 def push(element) @stack << element end |
#size ⇒ Integer
Returns size of stack.
61 62 63 |
# File 'lib/collection_utils/stack.rb', line 61 def size return @stack.size end |