I’m just trying to exclude a couple of child elements by class from a selection of all the children of a node
page.css('div.parent > *').each do |child|
if (child["class"].content != 'this' && child["class"].content != 'that')
array.push(child.text.to_s)
end
end
I know this not the write syntax, but have been unable to find how to select an elements class, as opposed to selects and element by class.
The
cssmethod gives youNokogiri::XML::Elementinstances and those get most of their behavior from theirNokogiri::XML::Nodeparent class. To get an attribute out of a Node, use[]:You could also use
if(child['class'] != 'this' && child['class'] != 'that')if that makes more sense to you.However,
classattributes can have multiple values so you might want to split them into pieces on whitespace:The intersection is just an easy way to see if the two arrays have any elements in common (i.e.
classescontains anything that you want to exclude).