Files
hello-algo/en/codes/ruby/chapter_stack_and_queue/array_stack.rb
T
Yudong Jin 2778a6f9c7 Translate all code to English (#1836)
* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
2025-12-31 07:44:52 +08:00

79 lines
1.3 KiB
Ruby

=begin
File: array_stack.rb
Created Time: 2024-04-06
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### Stack based on array ###
class ArrayStack
### Constructor ###
def initialize
@stack = []
end
### Get stack length ###
def size
@stack.length
end
### Check if stack is empty ###
def is_empty?
@stack.empty?
end
### Push ###
def push(item)
@stack << item
end
### Pop ###
def pop
raise IndexError, 'Stack is empty' if is_empty?
@stack.pop
end
### Access top element ###
def peek
raise IndexError, 'Stack is empty' if is_empty?
@stack.last
end
### Return list for printing ###
def to_array
@stack
end
end
### Driver Code ###
if __FILE__ == $0
# Access top of the stack element
stack = ArrayStack.new
# Elements push onto stack
stack.push(1)
stack.push(3)
stack.push(2)
stack.push(5)
stack.push(4)
puts "Stack stack = #{stack.to_array}"
# Return list for printing
peek = stack.peek
puts "Top element peek = #{peek}"
# Element pop from stack
pop = stack.pop
puts "Pop element pop = #{pop}"
puts "After pop, stack = #{stack.to_array}"
# Get the length of the stack
size = stack.size
puts "Stack length size = #{size}"
# Check if empty
is_empty = stack.is_empty?
puts "Is stack empty = #{is_empty}"
end