Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
refact
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed May 7, 2012
1 parent 1bc432c commit 0a4ae44
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions lib/abstractstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,20 @@ def initialize(limit=nil)
def_delegator :@list, :last, :peek

# @param [Object] value
# @return [Object] value
# @return [value]
def push(value)
if limit && (limit <= length)
raise OverFlow
else
@list.push value
self
end
raise OverFlow if limit && (limit <= length)

@list.push value
self
end

alias_method :<<, :push

# @return [Object]
def pop
if empty?
raise UnderFlow
else
@list.pop
end
raise UnderFlow if empty?

@list.pop
end

# @return [self]
Expand All @@ -88,22 +83,12 @@ def filo_each
alias_method :lifo_each, :filo_each

# @param [#to_int] pos
# @param [Symbol] start
# @param [Symbol] start - select one from :bottom or :top(:peek)
# @abstract - Define start point of index
def at(pos, start)
raise InvalidStackOperation if empty?

pos = pos.to_int

if pos < 0
if pos.abs > length
raise IndexError
end
else
if (pos + 1) > length
raise IndexError
end
end
validate_index pos

case start
when :bottom
Expand Down Expand Up @@ -138,7 +123,7 @@ def inspect

# @return [Boolean]
def ==(other)
(self.class == other.class) && @list == other._list
(self.class == other.class) && (@list == other._list)
end

def eql?(other)
Expand All @@ -162,5 +147,17 @@ def _list
def initialize_copy(original)
@list = @list.dup
end

def validate_index(pos)
if pos < 0
if pos.abs > length
raise IndexError
end
else
if (pos + 1) > length
raise IndexError
end
end
end

end

0 comments on commit 0a4ae44

Please sign in to comment.