instance method
Array#all?
() -> bool
(::Array::_Pattern[Elem] pattern) -> bool
() { (Elem obj) -> ::boolish } -> bool
all? → true or false
all?(object) → true or false
all? {|element| ... } → true or false
array.c
View on GitHub ↗
static VALUE
rb_ary_all_p(int argc, VALUE *argv, VALUE ary)
{
long i, len = RARRAY_LEN(ary);
rb_check_arity(argc, 0, 1);
if (!len) return Qtrue;
if (argc) {
if (rb_block_given_p()) {
rb_warn("given block not used");
}
for (i = 0; i < RARRAY_LEN(ary); ++i) {
if (!RTEST(rb_funcall(argv[0], idEqq, 1, RARRAY_AREF(ary, i)))) return Qfalse;
}
}
else if (!rb_block_given_p()) {
for (i = 0; i < len; ++i) {
if (!RTEST(RARRAY_AREF(ary, i))) return Qfalse;
}
}
else {
for (i = 0; i < RARRAY_LEN(ary); ++i) {
if (!RTEST(rb_yield(RARRAY_AREF(ary, i)))) return Qfalse;
}
}
return Qtrue;
}
Returns whether for every element of self, a given criterion is satisfied.
With no block and no argument, returns whether every element of self is truthy:
Example
[[], {}, '', 0, 0.0, Object.new].all? # => true # All truthy objects.
[[], {}, '', 0, 0.0, nil].all? # => false # nil is not truthy.
[[], {}, '', 0, 0.0, false].all? # => false # false is not truthy.
With argument object given, returns whether object === ele for every element ele in self:
Example
[0, 0, 0].all?(0) # => true [0, 1, 2].all?(1) # => false ['food', 'fool', 'foot'].all?(/foo/) # => true ['food', 'drink'].all?(/foo/) # => false
With a block given, calls the block with each element in self; returns whether the block returns only truthy values:
Example
[0, 1, 2].all? { |ele| ele < 3 } # => true [0, 1, 2].all? { |ele| ele < 2 } # => false
With both a block and argument object given, ignores the block and uses object as above.
Special case: returns true if self is empty (regardless of any given argument or block).
Related: see Methods for Querying.