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")   # == nil

Parameters:

  • selector (String) (defaults to: nil)

Returns:

  • (Nokogiri::XML::Element, nil)


104
105
106
# File 'lib/ryoba/nokogiri/xml/searchable.rb', line 104

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