Over a million developers have joined DZone.

How To Display Message When Collection Is Empty

·

>> for i in [1]
>>   puts i
>> end.any? or puts "empty!"
1
=> true
>> for i in [ ]
>>   puts i
>> end.any? or puts "empty!"
empty!
=> nil


short circuit evaluation is not intutitive? try this.


>> class Array
>>   alias :__empty? :empty?
>>   def empty?
>>     yield if __empty? and block_given?
>>     __empty?
>>   end
>> end
=> nil
>> for i in [1]
>>   puts i
>> end.empty? { puts "empty!" }
1
=> false
>> for i in [ ]
>>   puts i
>> end.empty? { puts "empty!" }
empty!
=> true
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}