Minor changes

This commit is contained in:
Vitor Oliveira 2020-12-27 14:02:46 -08:00
parent caa5afd97d
commit f0a01ee3da

View file

@ -7,19 +7,17 @@
class StackOverflowError < StandardError; end class StackOverflowError < StandardError; end
class Stack class Stack
def initialize(stack, limit) def initialize(limit, stack = [])
@stack = [] @stack = stack
@limit = limit @limit = limit
end end
attr_accessor :stack, :limit attr_accessor :stack, :limit
def push(item) def push(item)
if stack.count < limit raise StackOverflowError unless stack.count < limit
stack << item
else stack << item
raise StackOverflowError
end
end end
def pop def pop
@ -30,11 +28,11 @@ class Stack
stack.last stack.last
end end
def is_empty? def empty?
stack.count == 0 stack.count.zero?
end end
def is_full def full?
stack.count == limit stack.count == limit
end end
@ -47,9 +45,9 @@ class Stack
end end
end end
stack = Stack.new([], 10) stack = Stack.new(10, [])
puts stack.is_empty? puts stack.empty?
# => true # => true
stack.push(3) stack.push(3)
@ -57,7 +55,7 @@ stack.push(5)
stack.push(7) stack.push(7)
stack.push(9) stack.push(9)
puts stack.is_full puts stack.full?
# => false # => false
puts stack.contains?(5) puts stack.contains?(5)