From 1bf565ea0447f69d8ffcca756b82fa789f6afca0 Mon Sep 17 00:00:00 2001 From: ksss Date: Fri, 25 Nov 2016 22:25:48 +0900 Subject: Argument more strictly --- mrbgems/mruby-enum-ext/mrblib/enum.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'mrbgems/mruby-enum-ext/mrblib/enum.rb') diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb index 54db189f4..113b470a9 100644 --- a/mrbgems/mruby-enum-ext/mrblib/enum.rb +++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb @@ -215,13 +215,15 @@ module Enumerable # Returns the first element, or the first +n+ elements, of the enumerable. # If the enumerable is empty, the first form returns nil, 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 @@ -233,6 +235,8 @@ module Enumerable break if i == 0 end ary + else + raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..1)" end end -- cgit v1.2.3