I do realize that I can do
unless [1].empty?
But I'm wondering if there is a method?
I do realize that I can do
unless [1].empty?
But I'm wondering if there is a method?
No single method I know of is going to give you that in Ruby. Key word 'single':
[5].size.positive? => true
[5].size.nonzero? => 1
[].size.positive? => false
[].size.nonzero? => nil
Both of these are even more useful with the safe navigation operator, since nil returns falsy, meaning negative methods (like #empty?) break down a bit:
# nil considered 'not empty' isn't usually what you want
not_empty = proc{|obj| !obj&.empty? }
not_empty.call nil => true
not_empty.call [ ] => false
not_empty.call [5] => true
# gives different 3 answers for the 3 cases
# gives truthy/falsy values you'd probably expect
positive_size = proc{|obj| obj&.size&.positive? }
positive_size.call nil => nil
positive_size.call [ ] => false
positive_size.call [5] => true
# gives truthy/falsy values you'd probably expect
nonzero_size = proc{|obj| obj&.size&.nonzero? }
nonzero_size.call nil => nil
nonzero_size.call [ ] => nil
nonzero_size.call [5] => 1