diff options
| author | Yukihiro Matsumoto <[email protected]> | 2012-10-19 21:09:34 +0900 |
|---|---|---|
| committer | Yukihiro Matsumoto <[email protected]> | 2012-10-19 21:09:34 +0900 |
| commit | fd949663d2229419a1e53e69351205cd27157653 (patch) | |
| tree | 45465f1a596ee28d28bf3b7806a2b2eee8f3778f /mrblib/enum.rb | |
| parent | ac3b1c4c5a024301af54460e7846b880ed9de099 (diff) | |
| download | mruby-fd949663d2229419a1e53e69351205cd27157653.tar.gz mruby-fd949663d2229419a1e53e69351205cd27157653.zip | |
Enumerable#inject should handle empty enumerable; http://www.tbn.co.jp/blog/?p=813
Diffstat (limited to 'mrblib/enum.rb')
| -rw-r--r-- | mrblib/enum.rb | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/mrblib/enum.rb b/mrblib/enum.rb index 2699a18af..666ada294 100644 --- a/mrblib/enum.rb +++ b/mrblib/enum.rb @@ -200,13 +200,18 @@ module Enumerable # ISO 15.3.2.2.11 def inject(*args, &block) raise ArgumentError, "too many arguments" if args.size > 2 - flag = true # 1st element? - result = nil + if args.empty? + flag = true # no initial argument + result = nil + else + flag = false + result = args[0] + end self.each{|val| if flag - # 1st element - result = (args.empty?)? val: block.call(args[0], val) + # push first element as initial flag = false + result = val else result = block.call(result, val) end |
