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(<<-XML)
  <body>
    <div id="a">
      <div id="b">
        <img src="cat.jpg">
      </div>
    </div>
  </body>
XML

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