add Enumerable#min_by as well; ref #1912

This commit is contained in:
Yukihiro "Matz" Matsumoto
2014-03-22 16:55:47 +09:00
parent 04b5e705e0
commit 22d57105eb
2 changed files with 38 additions and 0 deletions
+34
View File
@@ -301,4 +301,38 @@ module Enumerable
end
max
end
##
# call-seq:
# enum.min_by {|obj| block } -> obj
# enum.min_by -> an_enumerator
#
# Returns the object in <i>enum</i> that gives the minimum
# value from the given block.
#
# If no block is given, an enumerator is returned instead.
#
# %w[albatross dog horse].min_by {|x| x.length } #=> "dog"
def min_by(&block)
return to_enum :min_by unless block_given?
first = true
min = nil
min_cmp = nil
self.each do |*val|
if first
min = val.__svalue
min_cmp = block.call(*val)
first = false
else
if (cmp = block.call(*val)) < min_cmp
min = val.__svalue
min_cmp = cmp
end
end
end
min
end
end
+4
View File
@@ -80,3 +80,7 @@ end
assert("Enumerable#max_by") do
assert_equal "albatross", %w[albatross dog horse].max_by { |x| x.length }
end
assert("Enumerable#min_by") do
assert_equal "dog", %w[albatross dog horse].min_by { |x| x.length }
end