Created
May 5, 2025 19:44
-
-
Save eri-b/e519e2374e76b57d6862de9a728fa7e8 to your computer and use it in GitHub Desktop.
Quick demo of all_but? method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def all_but?(number = 0) | |
count = 0 | |
if !block_given? | |
each { |item| count += 1 if item } | |
else | |
each { |item| count += 1 if yield(item) } | |
end | |
count >= length - number | |
end | |
end | |
puts [2, 4, -1, -3, 9].all_but?(1) { |val| val > -1 } | |
# => false | |
puts [2, 4, 1, -3, 9].all_but?(1, &:positive?) | |
# => true | |
puts [false, 2, 'hello'].all_but?(1) | |
# => true | |
puts [true, 2, 'hello', false, nil].all_but?(1) | |
# => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment