views:

38

answers:

2

I have an HTML document and I need to examine, whether some attribute is presented in element in question

Suppose, that the attribute is not presented.

When i say:

elem.has_attribute? "data-attr"

it returns nil instead of "false".

When i say:

elem["data-attr"].nil?

it returns "true", that is what i need.

But, when i say:

!elem["data-attr"].nil?

it retuns nil again.

When i say:

r = elem["data-attr"].nil?
r = !r

r gets "true" after the first line is executed

but, after the second line, "r" gets nil again

What the magic behind it ?

A: 

If

elem["data-attr"].nil?

returns true, why wouldn't you expect

!elem["data-attr"].nil?

to return nil?

Andrew Grimm
Because if elem["data-attr"].nil? is true, !true is false?
Jaco Pretorius
Yeah, but they both have falsiness.
Andrew Grimm