Has_attribute? problem

I have an HTML document and I need to examine if any attribute is represented in the element in question

Suppose the attribute is not represented.

When I speak:

elem.has_attribute? "data-attr"

it returns nil instead of "false".

When I speak:

elem["data-attr"].nil?

it returns true, this is what I need.

But when I say:

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

repeats zero again.

When I speak:

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

r gets "true" after the first line

but after the second line "r" again gets zero

What kind of magic is behind?

+3
source share
2 answers

Why not use:

elem["data-attr"].present?

or

elem["data-attr"].blank?
+1
source

If

elem["data-attr"].nil?

returns true, why don't you expect

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

to return nil?

+1
source

Source: https://habr.com/ru/post/1766146/


All Articles