class ArrayStack

Constants

VERSION

Attributes

size[R]

size. Getter method. Returns the ArrayStack's size.

Public Class Methods

new(value = nil) click to toggle source

Constructor Initialize the ArrayStack. @return A ArrayStack object.

# File lib/array-stack.rb, line 12
def initialize(value = nil)

  @size  = 0
  @array = []
  if (!value.nil?) then
    (self.push(value.dup()))
  end

end

Public Instance Methods

array=(array_stack) click to toggle source

array=(). Deeply copies the ArrayStack and assigns. @return Returns nil.

# File lib/array-stack.rb, line 79
def array=(array_stack)
  if (!array_stack.instance_of?(ArrayStack))
    raise(RuntimeError, "the argued object #{array_stack} is not an ArrayStack instance.")
  end
  if (size() > 0) then
    pop(size())
  end
  array_stack.array.each do |element|
    push(element.dup())
  end
  self.size = array_stack.size()
  return nil
end
empty?() click to toggle source

empty? Checks whether the ArrayStack is empty. @return True in the case the array is empty, and false otherwise.

# File lib/array-stack.rb, line 25
def empty?()
  return @array.empty?
end
pop(amount = 1) click to toggle source

pop. Removes the top elements and returns them. @param amount: the pop amount. Default is popping one element. @return In the case the size is at least two and the parameter is valid, popped is an array containing the top elements. In the case the size is 1 and the parameter is valid, pop returns the stored object. In the case the ArrayStack is empty, pop() returns nil.

# File lib/array-stack.rb, line 50
def pop(amount = 1)

  if (amount <= 0 || !amount.instance_of?(Fixnum))
    raise RuntimeError, "the amount argued was either negative or " +
        "the argument was not an integer."
  end
  if (size() >= 1 && amount <= size())
    popped    = @array.pop(amount)
    self.size -= amount
    if (popped.size() > 1)
      return popped
    else
      return popped[0]
    end
  else
    nil
  end

end
push(element) click to toggle source

push. Pushes an element on the ArrayStack's array and increments the size instance variable. @param element: the object the ArrayStack is taking. @return the duplicated “element” object.

# File lib/array-stack.rb, line 34
def push(element)

  duplicate = element.dup()
  @array.push(element.dup())
  @size += 1
  return duplicate

end
top() click to toggle source

top. Getter method. Returns the ArrayStack's top object duplicate.

# File lib/array-stack.rb, line 72
def top()
  @array.last().dup()
end

Protected Instance Methods

array() click to toggle source

array(). Getter method. Returns a duplicate ArrayStack.

# File lib/array-stack.rb, line 105
def array()

  duplicate = ArrayStack.new
  for object in @array
    duplicate.push(object.dup())
    duplicate.size += 1
  end
  return duplicate

end

Private Instance Methods

size=(value) click to toggle source

size=() Sets the size instance variable.

# File lib/array-stack.rb, line 97
def size=(value)
  @size = value
end