module Hamster class Stack def initialize(list = List.new) @list = list end # Returns true if the stack contains no items. def empty? @list.empty? end # Returns the number of items on the stack. def size @list.size end # Returns the item at the top of the stack. def top @list.car end # Returns a copy of self with the given item as the new top def push(item) self.class.new(@list.cons(item)) end # Returns a copy of self without the top item. def pop copy = @list.cdr if !copy.equal?(@list) self.class.new(copy) else self end end # Returns true if . eql? is synonymous with == def eql?(other) equal?(other) || (self.class.equal?(other.class) && @list.eql?(other.instance_eval{@list})) end alias :== :eql? # Returns self def dup self end alias :clone :dup end end