Class: RData::Stack
- Inherits:
-
Object
- Object
- RData::Stack
- Defined in:
- lib/rdata/stack.rb
Instance Method Summary collapse
-
#initialize ⇒ Stack
constructor
A new instance of Stack.
- #is_empty? ⇒ Boolean
- #pop ⇒ Object
- #push(x) ⇒ Object
- #top ⇒ Object
Constructor Details
#initialize ⇒ Stack
Returns a new instance of Stack.
15 16 17 18 |
# File 'lib/rdata/stack.rb', line 15 def initialize @stack = Array[] @top = 0 end |
Instance Method Details
#is_empty? ⇒ Boolean
38 39 40 |
# File 'lib/rdata/stack.rb', line 38 def is_empty? (@top == 0) ? 'true' : 'false' end |
#pop ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/rdata/stack.rb', line 29 def pop if self.is_empty? == "true" raise '[underflow] Cannot pop data from an empty stack' else @top = @top - 1 return @stack[@top + 1] end end |
#push(x) ⇒ Object
24 25 26 27 |
# File 'lib/rdata/stack.rb', line 24 def push(x) @top = @top + 1 @stack[@top] = x end |
#top ⇒ Object
20 21 22 |
# File 'lib/rdata/stack.rb', line 20 def top return @stack[@top] end |