summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-enum-ext/mrblib/enum.rb
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-enum-ext/mrblib/enum.rb')
-rw-r--r--mrbgems/mruby-enum-ext/mrblib/enum.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb
index d0e125850..e61062e3f 100644
--- a/mrbgems/mruby-enum-ext/mrblib/enum.rb
+++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb
@@ -267,4 +267,39 @@ module Enumerable
ary
end
alias collect_concat flat_map
+
+ ##
+ # call-seq:
+ # enum.max_by {|obj| block } -> obj
+ # enum.max_by -> an_enumerator
+ #
+ # Returns the object in <i>enum</i> that gives the maximum
+ # value from the given block.
+ #
+ # If no block is given, an enumerator is returned instead.
+ #
+ # %w[albatross dog horse].max_by { |x| x.length } #=> "albatross"
+
+ def max_by(&block)
+ return to_enum :max_by unless block_given?
+
+ first = true
+ max = nil
+ max_cmp = nil
+
+ self.each do |val|
+ if first
+ max = val
+ max_cmp = block.call(val)
+ first = false
+ else
+ if cmp = block.call(val) > max_cmp
+ max = val
+ max_cmp = cmp
+ end
+ end
+ end
+
+ max
+ end
end