If you include? == false

I have an array and a string:

$header = ["Date", "Time", "Site Name", "Computer Name"]
columnName = "esfjk sdhf sdf"

      

and I check if it contains $header

columnName

:

return if $header.include? columnName == false

      

The condition above always returns true

and the code continues even though the array does not contain a string.

I also have the same problem when $hash

is a hash but recordNum

is a number, for example 99999999

, which is not in it, and I do:

return if $hash.has_key? recordNum == false

      

Is there a reason for this?

+3


source to share


1 answer


Extraordinary.

$header.include? columnName == false

      

interpreted as

$header.include?(columnName == false)

      

usually,

$header.include?(false)

      



which is false. So what you want to do is the following:

$header.include?(columnName) == false

      

But in your specific case, I would do this (thanks, Alex Wayne):

return unless $header.include?(columnName)

      

And if you do that, you can return to the circular shape:

return unless $header.include? columnName

      

+7


source







All Articles