add Enumerable#drop

This commit is contained in:
skandhas
2013-03-19 11:16:33 +08:00
parent 4454576cc9
commit ab79941c51
2 changed files with 31 additions and 0 deletions
+21
View File
@@ -1,3 +1,24 @@
##
# 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
+10
View File
@@ -0,0 +1,10 @@
##
# 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