class RubyStructures::Stack

Public Class Methods

new() click to toggle source

Public: Creates a new instance of Stack with default values.

Examples

Stack.new

Returns a new instance of RubyStuctures::Stack.

# File lib/rubystructures/stack.rb, line 10
def initialize
        @storage = Array.new
end

Public Instance Methods

empty?() click to toggle source

Public: Boolean check to see if the stack is empty.

Examples

@stack.empty?
# => true

Returns true if the Stack is empty, false otherwise.

# File lib/rubystructures/stack.rb, line 88
def empty?
        @storage.size == 0
end
pop() click to toggle source

Public: Removes and returns the value from the top of the stack.

Examples

# If the stack has :size > 1, return the top value.
@stack.pop
# => 42

# If the stack is empty, returns nil.
@stack.pop
# => nil

Returns the value at the top of the stack, or nil if the stack is empty.

# File lib/rubystructures/stack.rb, line 59
def pop
        @storage.pop
end
push(value) click to toggle source

Public: Adds value to the Stack.

value - Ruby data type, can be of any class type.

Examples

@stack.push(42)
# => true

Returns true if the value was successfully added to the stack.

# File lib/rubystructures/stack.rb, line 38
def push(value)
        if @storage << value
                true
        else
                false
        end
end
size() click to toggle source

Public: Returns the Integer current size of the stack. We can rely on the Ruby Array to provide us with the correct stack size, which allows us to not have a :size instance variable.

Examples

@stack.size
# => 42

Returns the size of the stack.

# File lib/rubystructures/stack.rb, line 24
def size
        @storage.size
end
top() click to toggle source

Public: Peek at the value on the top of the stack without removing it.

Examples

@stack.top
# => 42

# If the stack is empty, returns nil.
@stack.top
# => nil

Returns the value at the stop of the Stack, but does not pop the value off the stack. Returns nil if the Stack is empty.

# File lib/rubystructures/stack.rb, line 76
def top
        @storage.last
end