Class: Stack

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

Instance Method Summary collapse

Constructor Details

#initializeStack

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

Returns:

  • (Boolean)


23
24
25
# File 'lib/data_structures/stack.rb', line 23

def empty?
  @store.empty?
end

#include?(el) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/data_structures/stack.rb', line 48

def include?(el)
  @store.include?(el)
end

#inspectObject



14
15
16
# File 'lib/data_structures/stack.rb', line 14

def inspect
  @store
end

#lengthObject



44
45
46
# File 'lib/data_structures/stack.rb', line 44

def length
  @store.length
end

#peekObject



40
41
42
# File 'lib/data_structures/stack.rb', line 40

def peek
  @store.last
end

#popObject



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_aObject



6
7
8
# File 'lib/data_structures/stack.rb', line 6

def to_a
  Array.new(@store)
end

#to_sObject



10
11
12
# File 'lib/data_structures/stack.rb', line 10

def to_s
  @store
end