Method: Nokogiri::XML::Node#matches!

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

#matches!(selector) ⇒ self

Like Node#matches?, but returns the Node if selector matches, and raises an error otherwise.

Examples:

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

xml.at("#a").matches(".c")!  # == Node div#a.c
xml.at("#b").matches(".c")!  # raise error

Parameters:

  • selector (String)

Returns:

  • (self)

Raises:



81
82
83
84
85
86
87
# File 'lib/ryoba/nokogiri/xml/node.rb', line 81

def matches!(selector)
  if !self.matches?(selector)
    abbreviated = self.to_html[/[^>]+>/]
    raise Ryoba::Error.new("Node #{abbreviated} does not match #{selector.inspect}")
  end
  self
end