Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

has_attribute? problem

Tags:

ruby

nokogiri

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 ?

like image 331
AntonAL Avatar asked Aug 26 '26 23:08

AntonAL


1 Answers

Why not use:

elem["data-attr"].present?

or

elem["data-attr"].blank?
like image 125
Taryn East Avatar answered Aug 29 '26 14:08

Taryn East