Commit 4256130e authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #3293 from ksss/enum-first

Fix incompatibility for Enumerable#first
parents f6663506 1bf565ea
...@@ -215,21 +215,28 @@ module Enumerable ...@@ -215,21 +215,28 @@ module Enumerable
# Returns the first element, or the first +n+ elements, of the enumerable. # Returns the first element, or the first +n+ elements, of the enumerable.
# If the enumerable is empty, the first form returns <code>nil</code>, and the # If the enumerable is empty, the first form returns <code>nil</code>, and the
# second form returns an empty array. # second form returns an empty array.
def first(n=NONE) def first(*args)
if n == NONE case args.length
when 0
self.each do |*val| self.each do |*val|
return val.__svalue return val.__svalue
end end
return nil return nil
else when 1
a = [] n = args[0]
i = 0 raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
i = n.to_int
raise ArgumentError, "attempt to take negative size" if i < 0
ary = []
return ary if i == 0
self.each do |*val| self.each do |*val|
break if n<=i ary << val.__svalue
a.push val.__svalue i -= 1
i += 1 break if i == 0
end end
a ary
else
raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..1)"
end end
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