Commit ab79941c authored by skandhas's avatar skandhas

add Enumerable#drop

parent 4454576c
##
# Enumerable
#
module Enumerable
##
# call-seq:
# enum.drop(n) -> array
#
# Drops first n elements from <i>enum</i>, and returns rest elements
# in an array.
#
# a = [1, 2, 3, 4, 5, 0]
# a.drop(3) #=> [4, 5, 0]
def drop(n)
raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer
raise ArgumentError, "attempt to drop negative size" if n < 0
ary = []
self.each {|e| n == 0 ? ary << e : n -= 1 }
ary
end
end
##
# Enumerable(Ext) Test
assert("Enumrable#drop") do
a = [1, 2, 3, 4, 5, 0]
assert_equal a.drop(3), [4, 5, 0]
assert_equal a.drop(6), []
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment