Method: Nokogiri::XML::Searchable#search!

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

#search!(*queries) ⇒ Nokogiri::XML::NodeSet

Like Searchable#search, but raises an error if there are no results.

Examples:

xml = Nokogiri::XML(<<-XML)
  <body>
    <div id="a" />
    <div id="b" />
  </body>
XML

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

Parameters:

  • queries (Array<String>)

Returns:

  • (Nokogiri::XML::NodeSet)

Raises:



21
22
23
24
25
26
27
# File 'lib/ryoba/nokogiri/xml/searchable.rb', line 21

def search!(*queries)
  results = self.search(*queries)
  if results.empty?
    raise Ryoba::Error.new("No elements matching #{queries.map(&:inspect).join(" OR ")}")
  end
  results
end