Merge pull request #1922 from suzukaze/add-enum.none

Add Enumerable.none?
This commit is contained in:
Yukihiro "Matz" Matsumoto
2014-03-23 16:35:55 +09:00
2 changed files with 34 additions and 0 deletions
+26
View File
@@ -413,4 +413,30 @@ module Enumerable
end
[min, max]
end
##
# call-seq:
# enum.none? [{ |obj| block }] -> true or false
#
# Passes each element of the collection to the given block. The method
# returns <code>true</code> if the block never returns <code>true</code>
# for all elements. If the block is not given, <code>none?</code> will return
# <code>true</code> only if none of the collection members is true.
#
# %w(ant bear cat).none? { |word| word.length == 5 } #=> true
# %w(ant bear cat).none? { |word| word.length >= 4 } #=> false
# [].none? #=> true
# [nil, false].none? #=> true
# [nil, true].none? #=> false
def none?(&block)
self.each do |*val|
if block
return false if block.call(*val)
else
return false if val.__svalue
end
end
true
end
end
+8
View File
@@ -94,3 +94,11 @@ end
assert("Enumerable#minmax_by") do
assert_equal ["dog", "albatross"], %w(albatross dog horse).minmax_by { |x| x.length }
end
assert("Enumerable#none?") do
assert_true %w(ant bear cat).none? { |word| word.length == 5 }
assert_false %w(ant bear cat).none? { |word| word.length >= 4 }
assert_true [].none?
assert_true [nil, false].none?
assert_false [nil, true].none?
end