Commit 85d20127 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

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

Add Enumerable.none?
parents ff666663 d8d47b80
......@@ -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
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment