def check_nill(array1)
p = Proc.new {array1.any? { |x| x.nil? }}
if p == true
puts "true"
else
puts "false"
end
end
it always give me false and i dont know why
CodePudding user response:
Proc.new
does not actually run the code. You have to pass it over, or call it using Proc#call
.
is_nil = Proc.new {|item| item.nil?}
["a", "b", nil].any?(is_nil)
# => true
Your code always return false
because p
was an instance of Proc
, and not the result of the block.
p = Proc.new {array1.any? { |x| x.nil? }} # <Proc:0x0000000>
if p == true # <Proc:0x0000000> == true? no
puts "true"
else
puts "false" # prints false
end
CodePudding user response:
You are overdoing it, creating a proc inside a method and then not calling it.
check_nil = proc {|ar| ar.any?{|x| x.nil?} # or just proc {|ar| ar.any?(nil)}
puts check_nil.call([1,2,nil,3]) # => true