Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -41,25 +41,15 @@ class Stack
# If passing a number _n_, removes the top _n_ elements, and returns
# an Array of them. If this Stack contains fewer than _n_ elements,
# returns them all. If this Stack is empty, returns an empty Array.
nil
def_delegator :@ary, :pop
if ([].pop(0) rescue false)
# Ruby >= 1.8.7
def_delegator :@ary, :pop
else
# Ruby < 1.8.7
def pop(*args) # :nodoc:
case len = args.length
when 0
@ary.pop
when 1
n = [@ary.length, args.first].min
@ary.slice!(-n, n)
else
raise ArgumentError, "wrong number of arguments (#{len} for 0..1)"
end
end
end
##
# :method: top
# :call-seq:
# top -> obj or nil
# top(n) -> ary
# Returns the topmost element without modifying the stack.
def_delegator :@ary, :last, :top
##
# :method: empty?

View file

@ -1,12 +1,19 @@
s = Stack.new
s.empty? # => true
s.pop # => nil
s.pop(1) # => []
s.push(1) # => Stack[1]
s.push(2, 3) # => Stack[1, 2, 3]
s.pop # => 3
s.pop(1) # => [2]
s.empty? # => false
p s = Stack.new # => Stack[]
p s.empty? # => true
p s.size # => 0
p s.top # => nil
p s.pop # => nil
p s.pop(1) # => []
p s.push(1) # => Stack[1]
p s.push(2, 3) # => Stack[1, 2, 3]
p s.top # => 3
p s.top(2) # => [2, 3]
p s # => Stack[1, 2, 3]
p s.size # => 3
p s.pop # => 3
p s.pop(1) # => [2]
p s.empty? # => false
s = Stack[:a, :b, :c]
s.pop # => :c
p s = Stack[:a, :b, :c] # => Stack[:a, :b, :c]
p s << :d # => Stack[:a, :b, :c, :d]
p s.pop # => :d