one?()
public
Passes each element of the collection to the given block. The method
returns true if the block returns true exactly once. If
the block is not given, one?
will return true only if exactly one of the collection members is
true.
%w{ant bear cat}.one? {|word| word.length == 4}
%w{ant bear cat}.one? {|word| word.length > 4}
%w{ant bear cat}.one? {|word| word.length < 4}
[ nil, true, 99 ].one?
[ nil, true, false ].one?
Show source
/*
* 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
*
*/
static VALUE
enum_one(obj)
VALUE obj;
{
VALUE result = Qundef;
rb_block_call(obj, id_each, 0, 0, rb_block_given_p() ? one_iter_i : one_i, (VALUE)&result);
if (result == Qundef) return Qfalse;
return result;
}