mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
add Enumerable#drop
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user