Method: Nokogiri::XML::Searchable#ancestor!

Defined in:
lib/ryoba/nokogiri/xml/searchable.rb

#ancestor!(selector = nil) ⇒ Nokogiri::XML::Element

Like Searchable#ancestors!, but returns only the first matching ancestor.

Examples:

xml = Nokogiri::XML("  <body>\n    <div id=\"a\">\n      <div id=\"b\">\n        <img src=\"cat.jpg\">\n      </div>\n    </div>\n  </body>\n")

xml.at("img").ancestor!("div")  # == Node div#b
xml.at("img").ancestor!("#a")   # == Node div#a
xml.at("img").ancestor!("#z")   # raise error

Parameters:

  • selector (String) (defaults to: nil)

Returns:

  • (Nokogiri::XML::Element)

Raises:



130
131
132
# File 'lib/ryoba/nokogiri/xml/searchable.rb', line 130

def ancestor!(selector = nil)
  self.ancestors!(selector).first
end