array1= [{},nil,[]]
array2=[{},23,45]
These are my input
I used .nil? and .empty? function. The the both are gives false
but the first one is nil
I need help with this this
array.nil? checks if array is nil, not if it contains a nil value. Similarly, array.empty? checks if array contains no elements.
If you want to check if any element is nil, use any?.
array.any? { |element| element.nil? }
That can be shortened to array.any?(nil).
arr != arr.compact.arr.any?(NilClass)? Also, "shortened" may not be the right word.a.group_by(&:nil?).key?(true) ;)Actually you are mis-understanding the functionality of .nil? and .empty?. “.nil?” Will only return true for “nil” values i.e. “nil.nil? => true” but “[].nil? => false”.
Coming to “.empty?” if you see your variables “array1 and array2” both have some elements, it doesn’t matter if they have nil values, empty array or hashes, but what matters is both of them has 3 elements each. This will help you understanding the difference.
nil?checks whether the receiver isnil, not if it contains it.