use to_int to integer duck-type check

parent 1e73181d
...@@ -13,9 +13,10 @@ module Enumerable ...@@ -13,9 +13,10 @@ module Enumerable
# a.drop(3) #=> [4, 5, 0] # a.drop(3) #=> [4, 5, 0]
def drop(n) def drop(n)
raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
raise ArgumentError, "attempt to drop negative size" if n < 0 raise ArgumentError, "attempt to drop negative size" if n < 0
n = n.to_int
ary = [] ary = []
self.each {|*val| n == 0 ? ary << val.__svalue : n -= 1 } self.each {|*val| n == 0 ? ary << val.__svalue : n -= 1 }
ary ary
...@@ -51,9 +52,10 @@ module Enumerable ...@@ -51,9 +52,10 @@ module Enumerable
# a.take(3) #=> [1, 2, 3] # a.take(3) #=> [1, 2, 3]
def take(n) def take(n)
raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
raise ArgumentError, "attempt to take negative size" if n < 0 raise ArgumentError, "attempt to take negative size" if n < 0
n = n.to_int
ary = [] ary = []
self.each do |*val| self.each do |*val|
break if ary.size >= n break if ary.size >= n
...@@ -102,10 +104,11 @@ module Enumerable ...@@ -102,10 +104,11 @@ module Enumerable
# [8, 9, 10] # [8, 9, 10]
def each_cons(n, &block) def each_cons(n, &block)
raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
raise ArgumentError, "invalid size" if n <= 0 raise ArgumentError, "invalid size" if n <= 0
ary = [] ary = []
n = n.to_int
self.each do |*val| self.each do |*val|
ary.shift if ary.size == n ary.shift if ary.size == n
ary << val.__svalue ary << val.__svalue
...@@ -128,10 +131,11 @@ module Enumerable ...@@ -128,10 +131,11 @@ module Enumerable
# [10] # [10]
def each_slice(n, &block) def each_slice(n, &block)
raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
raise ArgumentError, "invalid slice size" if n <= 0 raise ArgumentError, "invalid slice size" if n <= 0
ary = [] ary = []
n = n.to_int
self.each do |*val| self.each do |*val|
ary << val.__svalue ary << val.__svalue
if ary.size == n if ary.size == n
...@@ -560,10 +564,9 @@ module Enumerable ...@@ -560,10 +564,9 @@ module Enumerable
end end
end end
else else
unless n.kind_of? Integer raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
raise TypeError, "expected Integer for 1st argument"
end
n = n.to_int
self.each do|*val| self.each do|*val|
ary.push val ary.push val
end 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