diff options
Diffstat (limited to 'mrblib/enum.rb')
| -rw-r--r-- | mrblib/enum.rb | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/mrblib/enum.rb b/mrblib/enum.rb index 2699a18af..2774bc856 100644 --- a/mrblib/enum.rb +++ b/mrblib/enum.rb @@ -200,19 +200,30 @@ 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 Symbol === args[-1] + sym = args[-1] + block = ->(x,y){x.send(sym,y)} + args.pop + end + 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 } result end + alias reduce inject ## # Alias for collect |
