Method: Nokogiri::XML::Searchable#ancestors!

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

#ancestors!(selector = nil) ⇒ Nokogiri::XML::NodeSet

Like Searchable#ancestors, but raises an error if there are no matching ancestors.

Examples:

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

xml.at("img").ancestors!("div")  # == NodeSet [div#b, div#a]
xml.at("img").ancestors!("#a")   # == NodeSet [div#a]
xml.at("img").ancestors!("#z")   # raise error

Parameters:

  • selector (String) (defaults to: nil)

Returns:

  • (Nokogiri::XML::NodeSet)

Raises:



76
77
78
79
80
81
82
# File 'lib/ryoba/nokogiri/xml/searchable.rb', line 76

def ancestors!(selector = nil)
  results = self.ancestors(selector)
  if results.empty?
    raise Ryoba::Error.new("No ancestors matching #{selector.inspect}")
  end
  results
end