Skip to content

Instantly share code, notes, and snippets.

@nikukyugamer
Last active June 17, 2022 05:20
Show Gist options
  • Save nikukyugamer/00cb276e3b47fd58417c6099ac1517e0 to your computer and use it in GitHub Desktop.
Save nikukyugamer/00cb276e3b47fd58417c6099ac1517e0 to your computer and use it in GitHub Desktop.
Stack を簡易的に作る
class Stack
attr_reader :stack
def initialize
@stack = []
end
def push(element)
@stack.push(element)
end
def pop
pop_value = @stack.first
@stack.delete_at(0)
pop_value
end
def peek
@stack.first
end
def empty?
@stack.empty?
end
def size
@stack.size
end
end
s = Stack.new
s.push("a")
s.push("b")
s.pop #=> a
s.stack #=> ["b"]
s.peek #=> b
s.empty? #=> false
s.size #=> 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment