[].all? doesn't take a block?

Anybody know what’s going on here?

[].all?
=> true

After 5 years of using ruby, it still
surprises me at times–shouldn’t that require a block? :slight_smile:
-roger-

Roger P. wrote in post #1005933:

Anybody know what’s going on here?

[].all?
=> true

After 5 years of using ruby, it still
surprises me at times–shouldn’t that require a block? :slight_smile:
-roger-

class Enumerable

enum.all? [{|obj| block } ] → true or false


If the block is not given, Ruby adds an implicit block of {|obj| obj}
(that is all? will return true only if none of the collection members
are false or nil.)

None of your collection members are false or nil, so all? returns true.

7stud – wrote in post #1006058:

Roger P. wrote in post #1005933:

Anybody know what’s going on here?

[].all?
=> true

After 5 years of using ruby, it still
surprises me at times–shouldn’t that require a block? :slight_smile:
-roger-

class Enumerable

enum.all? [{|obj| block } ] → true or false


If the block is not given, Ruby adds an implicit block of {|obj| obj}
(that is all? will return true only if none of the collection members
are false or nil.)

None of your collection members are false or nil, so all? returns true.

I think the result is counterintuitive.
[].none? also returns true.

Joey Z. wrote in post #1006108:

Anybody know what’s going on here?

[].all?
=> true

None of your collection members are false or nil, so all? returns true.

I think the result is counterintuitive.

Let’s check Haskell:

any (==4) [3, 4]
=> True
any (==4) []
=> False
all (==4) [3, 4]
=> False
all (==4) []
=> True