Commit 919ca8f4 authored by Jun Hiroe's avatar Jun Hiroe

Add Enumerable#one?

parent e2bab8c7
......@@ -439,4 +439,33 @@ module Enumerable
end
true
end
##
# call-seq:
# enum.one? [{ |obj| block }] -> true or false
#
# Passes each element of the collection to the given block. The method
# returns <code>true</code> if the block returns <code>true</code>
# exactly once. If the block is not given, <code>one?</code> will return
# <code>true</code> only if exactly one of the collection members is
# true.
#
# %w(ant bear cat).one? { |word| word.length == 4 } #=> true
# %w(ant bear cat).one? { |word| word.length > 4 } #=> false
# %w(ant bear cat).one? { |word| word.length < 4 } #=> false
# [nil, true, 99].one? #=> false
# [nil, true, false].one? #=> true
#
def one?(&block)
count = 0
self.each do |*val|
if block
count += 1 if block.call(*val)
else
count += 1 if val.__svalue
end
end
count == 1 ? true : false
end
end
......@@ -102,3 +102,12 @@ assert("Enumerable#none?") do
assert_true [nil, false].none?
assert_false [nil, true].none?
end
assert("Enumerable#one?") do
assert_true %w(ant bear cat).one? { |word| word.length == 4 }
assert_false %w(ant bear cat).one? { |word| word.length > 4 }
assert_false %w(ant bear cat).one? { |word| word.length < 4 }
assert_false [nil, true, 99].one?
assert_true [nil, true, false].one?
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