title: Is there some advantage to filling a stack with nils and interpreting the "top" as the last non-nil value?
body: <p>While working on a <a href="http://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/33-advanced-arrays/lessons/86-stacks-and-queues#solution4117" rel="nofollow">rubymonk</a> exercise, I am asked to implement a stack with a hard size limit. It should return 'nil' if I try to push too many values, or if I try to pop an empty stack.</p>
  
  <p>My solution is below, followed by their solution. Mine passes every test I can give it in my IDE, while it fails rubymonk's test. But that isn't my question.</p>
  
  <p>Question is, why did they choose to fill the stack with nils instead of letting it shrink and grow like it does in my version?</p>
  
  <p>It just makes their code more complex.</p>
  
  <p>Here's my solution:</p>
  
  <pre><code>class Stack
    def initialize(size)
      @max = size
      @store = Array.new
    end
  
    def pop
      empty? ? nil : @store.pop
    end
  
    def push(element)
      return nil if full?
      @store.push(element)
    end
  
    def size
      @store.size
    end
  
    def look
      @store.last
    end
  
    private
  
    def full?
      @store.size == @max
    end
  
    def empty?
      @store.size == 0
    end
  end
  </code></pre>
  
  <p>and here is the accepted answer</p>
  
  <pre><code>class Stack
    def initialize(size)
      @size = size
      @store = Array.new(@size)
      @top = -1
    end
  
    def pop
      if empty?
        nil
      else
        popped = @store[@top]
        @store[@top] = nil
        @top = @top.pred
        popped
      end
    end
  
    def push(element)
      if full? or element.nil?
        nil
      else
        @top = @top.succ
        @store[@top] = element
        self
      end
    end
  
    def size
      @size
    end
  
    def look
      @store[@top]
    end
  
    private
  
    def full?
      @top == (@size - 1)
    end
  
    def empty?
      @top == -1
    end
  end
  </code></pre>