Class: Stack
- Inherits:
-
Object
- Object
- Stack
- Defined in:
- lib/data_structures/stack.rb
Instance Method Summary collapse
- #<<(el) ⇒ Object
- #==(other_stack) ⇒ Object
- #empty? ⇒ Boolean
- #include?(el) ⇒ Boolean
-
#initialize ⇒ Stack
constructor
A new instance of Stack.
- #inspect ⇒ Object
- #length ⇒ Object
- #peek ⇒ Object
- #pop ⇒ Object
- #push(el) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Stack
Returns a new instance of Stack.
2 3 4 |
# File 'lib/data_structures/stack.rb', line 2 def initialize @store = Array.new end |
Instance Method Details
#<<(el) ⇒ Object
32 33 34 |
# File 'lib/data_structures/stack.rb', line 32 def <<(el) @store << el end |
#==(other_stack) ⇒ Object
18 19 20 21 |
# File 'lib/data_structures/stack.rb', line 18 def ==(other_stack) return false unless other_stack.is_a?(Stack) @store == other_stack.send(:store) end |
#empty? ⇒ Boolean
23 24 25 |
# File 'lib/data_structures/stack.rb', line 23 def empty? @store.empty? end |
#include?(el) ⇒ Boolean
48 49 50 |
# File 'lib/data_structures/stack.rb', line 48 def include?(el) @store.include?(el) end |
#inspect ⇒ Object
14 15 16 |
# File 'lib/data_structures/stack.rb', line 14 def inspect @store end |
#length ⇒ Object
44 45 46 |
# File 'lib/data_structures/stack.rb', line 44 def length @store.length end |
#peek ⇒ Object
40 41 42 |
# File 'lib/data_structures/stack.rb', line 40 def peek @store.last end |
#pop ⇒ Object
36 37 38 |
# File 'lib/data_structures/stack.rb', line 36 def pop @store.pop end |
#push(el) ⇒ Object
27 28 29 30 |
# File 'lib/data_structures/stack.rb', line 27 def push(el) @store.push(el) self end |
#to_a ⇒ Object
6 7 8 |
# File 'lib/data_structures/stack.rb', line 6 def to_a Array.new(@store) end |
#to_s ⇒ Object
10 11 12 |
# File 'lib/data_structures/stack.rb', line 10 def to_s @store end |