Skip to content

Commit

Permalink
Argument more strictly
Browse files Browse the repository at this point in the history
  • Loading branch information
ksss committed Nov 25, 2016
1 parent c28a963 commit 1bf565e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions mrbgems/mruby-enum-ext/mrblib/enum.rb
Expand Up @@ -215,13 +215,15 @@ def sort_by(&block)
# 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
# second form returns an empty array.
def first(n=NONE)
if n == NONE
def first(*args)
case args.length
when 0
self.each do |*val|
return val.__svalue
end
return nil
else
when 1
n = args[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
Expand All @@ -233,6 +235,8 @@ def first(n=NONE)
break if i == 0
end
ary
else
raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..1)"
end
end

Expand Down

0 comments on commit 1bf565e

Please sign in to comment.