Skip to content

Instantly share code, notes, and snippets.

@eri-b
Created May 5, 2025 19:44
Show Gist options
  • Save eri-b/e519e2374e76b57d6862de9a728fa7e8 to your computer and use it in GitHub Desktop.
Save eri-b/e519e2374e76b57d6862de9a728fa7e8 to your computer and use it in GitHub Desktop.
Quick demo of all_but? method
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