Class: RubyLabs::LinearDSLab::Stack
- Inherits:
-
Object
- Object
- RubyLabs::LinearDSLab::Stack
- Defined in:
- lib/lineardslab.rb
Overview
Ruby Array-based implementation of a Stack
Instance Attribute Summary collapse
-
#drawing ⇒ Object
Struct object to store the attributes of the drawn visualization.
-
#items ⇒ Object
readonly
array used to store the values in the stack.
Instance Method Summary collapse
-
#count ⇒ Object
returns the number of items currently in the stack.
-
#initialize ⇒ Stack
constructor
class constructor.
-
#inspect ⇒ Object
return a string representation of the array.
-
#peek ⇒ Object
peek method.
-
#pop ⇒ Object
pop method.
-
#push(value) ⇒ Object
push method.
-
#to_s ⇒ Object
returns a string representation of the array.
Constructor Details
#initialize ⇒ Stack
class constructor
75 76 77 |
# File 'lib/lineardslab.rb', line 75 def initialize @items = Array.new end |
Instance Attribute Details
#drawing ⇒ Object
Struct object to store the attributes of the drawn visualization
72 73 74 |
# File 'lib/lineardslab.rb', line 72 def drawing @drawing end |
#items ⇒ Object (readonly)
array used to store the values in the stack
70 71 72 |
# File 'lib/lineardslab.rb', line 70 def items @items end |
Instance Method Details
#count ⇒ Object
returns the number of items currently in the stack
116 117 118 |
# File 'lib/lineardslab.rb', line 116 def count return items.count end |
#inspect ⇒ Object
return a string representation of the array
106 107 108 |
# File 'lib/lineardslab.rb', line 106 def inspect return to_s end |
#peek ⇒ Object
peek method. if a drawing exists, redraw the canvas
89 90 91 92 93 94 |
# File 'lib/lineardslab.rb', line 89 def peek if @drawing view_stack(self, true) end return items.first end |
#pop ⇒ Object
pop method. if a drawing exists, redraw the canvas
80 81 82 83 84 85 86 |
# File 'lib/lineardslab.rb', line 80 def pop value = items.shift if @drawing view_stack(self, false) end return value end |
#push(value) ⇒ Object
push method. if a drawing exists, redraw the canvas
97 98 99 100 101 102 103 |
# File 'lib/lineardslab.rb', line 97 def push(value) output = items.unshift(value) if @drawing view_stack(self, false) end return output end |
#to_s ⇒ Object
returns a string representation of the array
111 112 113 |
# File 'lib/lineardslab.rb', line 111 def to_s return @items.inspect end |