Method: Enumerable#all?

Defined in:
enum.c

#all? {|obj| ... } ⇒ Boolean #all?(pattern) ⇒ Boolean

Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of { |obj| obj } which will cause #all? to return true when none of the collection members are false or nil.

If instead a pattern is supplied, the method returns whether pattern === element for every collection member.

%w[ant bear cat].all? { |word| word.length >= 3 } #=> true
%w[ant bear cat].all? { |word| word.length >= 4 } #=> false
%w[ant bear cat].all?(/t/)                        #=> false
[1, 2i, 3.14].all?(Numeric)                       #=> true
[nil, true, 99].all?                              #=> false
[].all?                                           #=> true

Overloads:

  • #all? {|obj| ... } ⇒ Boolean

    Yields:

    • (obj)

    Returns:

    • (Boolean)
  • #all?(pattern) ⇒ Boolean

    Returns:

    • (Boolean)


1377
1378
1379
1380
1381
1382
1383
1384
# File 'enum.c', line 1377

static VALUE
enum_all(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo = MEMO_ENUM_NEW(Qtrue);
    WARN_UNUSED_BLOCK(argc);
    rb_block_call(obj, id_each, 0, 0, ENUMFUNC(all), (VALUE)memo);
    return memo->v1;
}