mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Add Enumerable#one?
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user