I'm trying to solve this assignment:
Create a Proc that will check whether a given array of elements are nil or not. Use
.nil?
to evaluate elements. Print 'true' if the elements arenil
and 'false' if the element has a [truthy] value.
This is my attempt:
def check_nil(array1)
p = Proc.new {|x| x.nil?}
res= p.call(array1)
if res == true
return "true"
else
return "false"
end
end
array1 = [{},nil,[]]
result = check_nil(array1)
puts "#{result}"
Here actually, the output should be "true" but this code gives "false" as output. Can someone explain the reason?
[nil,nil,nil].all? nil
No proc.