2

Response list returns empty nested array: [[], [], []]

How better test with ruby that following nested array is empty?

3
  • All nested arrays or some specific? Commented Oct 5, 2022 at 10:23
  • All nested arrays Commented Oct 5, 2022 at 10:27
  • How do you define an "empty nested array"? The one you have in your question is most definitely not empty, it contains three elements. Commented Oct 5, 2022 at 21:12

2 Answers 2

4

You can use Array#empty?

To check all nested arrays are empty with Array#all?

[[], [], []].all?(&:empty?)
# => true

[[1], [2], []].all?(&:empty?)
# => false

[[1], [2], [3]].all?(&:empty?)
# => false

To check at least one nested is empty with Array#any?

[[], [], []].any?(&:empty?)
# => true

[[1], [2], []].any?(&:empty?)
# => true

[[1], [2], [3]].any?(&:empty?)
# => false
Sign up to request clarification or add additional context in comments.

Comments

3

If you want to handle deeply nested arrays, you probably want to flatten your array first:

[[], [], []].flatten.empty?
=> true

[[], [[], [[]]]].flatten.empty?
=> true

[[], [[], [[1]]]].flatten.empty?
=> false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.