I'm using Ruby 2.4. How do I check if all elements of my array satisfy a condition? I have
cond = true
arr.each do |e|
if e.nil? || e.to_i < 5
cond = false
break
end
end
but I feel like there's a more efficient way to do this.
You can rewrite that as
arr.all? { |e| e.to_i >= 5 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With