Class: RubyLabs::LinearDSLab::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/lineardslab.rb

Overview

Ruby Array-based implementation of a Stack

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStack

class constructor



75
76
77
# File 'lib/lineardslab.rb', line 75

def initialize
	@items = Array.new
end

Instance Attribute Details

#drawingObject

Struct object to store the attributes of the drawn visualization



72
73
74
# File 'lib/lineardslab.rb', line 72

def drawing
  @drawing
end

#itemsObject (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

#countObject

returns the number of items currently in the stack



116
117
118
# File 'lib/lineardslab.rb', line 116

def count
	return items.count
end

#inspectObject

return a string representation of the array



106
107
108
# File 'lib/lineardslab.rb', line 106

def inspect
	return to_s
end

#peekObject

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

#popObject

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_sObject

returns a string representation of the array



111
112
113
# File 'lib/lineardslab.rb', line 111

def to_s	
	return @items.inspect
end