Patchouli's Computatrunomicon

Search IconIcon to open search

predicate-enumerable-methods

Last updated June 5, 2022.

# Metadata

2022-06-04 18:20 | predicate-enumerable-methods | Doriel Rivalet

# Content

a predicate method is indicated by a question mark (?) at the end of the method name and returns either true or false

all? method will return true if you call it on an empty array.

Example:

1
2
3
[].all? { |s| s.size == 1 }

# true

Explanation:

Since NO elements are false then all elements must be true.

any? works like the inverse of empty?. But that won’t work for every situation.

Because this is asking the question:

“Are there any TRUTHY elements inside this array?”

If your array includes only non-truthy (nil/false) values you’ll get false, but the array is not really empty.

You can also pass a block to this method:

1
2
3
[1,2,3].any? { |n| n > 0 }

# true

This will check if n > 0 is true for AT LEAST one element.

(any? / all? / none? / one?) also take an argument which works  like grep’s argument.

Here’s what I mean:

1
2
3
4
5
6
7
[:orange, :apple, :coconut].any?(Symbol)

# true

[1,2,3].all?(1..10)

# true

This is a nice little shortcut if you want to check for a class, regular expression or a range.

# Sources

Own notes

https://www.theodinproject.com/lessons/ruby-predicate-enumerable-methods

https://www.rubyguides.com/2018/10/any-all-none-one/

a lot of people think that .any? checks if an enumerable contains any elements, and .none? checks if there are no elements (like .empty?). There’s a nasty gotcha lurking in that interpretation.

These don’t check whether elements exist, but whether they make the block return a truthy value (or if you don’t pass a block, then whether they are truthy). So frex [nil, false].any? is false, and [nil, false].none? is true.

arr.any? is same as asking, is any truthy?

arr.none? is same as asking, is any existing?

# Content Lists

If you prefer browsing the contents of this site through a list instead of a graph, you can find content lists here too:

# Knowledge Index


Interactive Graph