0

I'm parsing an XML file with Nokogiri and I only want to save the variable if a value exists for it in the XML. Right now, I'm having it rescued with a nil, which is generating the wrong results for me. So, how can I set my statement to only save if both of these have actual values (not nil)?

port.xpath("service").each do |service|
            product = service.at_xpath("@product").value rescue nil
            version = service.at_xpath("@version").value rescue nil

Currently, when the file is parsed both of the values could be nil, which then matches to everything! Instead, I really need my parser to just pass over the values if it can't find a product & version match.

For instance, let's say the XML says that the service is product vFTP and version 2.2.1. This is good, because it matches correctly (vFTP 2.2.1).

However, if version nil, I now have a problem because it will match with all versions of vFTP now.

An if statement to check for defined? seems logical, but for some reason that doesn't seem right to me.

Godzilla74
  • 2,358
  • 1
  • 31
  • 67

1 Answers1

0

Here are a couple ways. On the next line:

next unless [product, version].all?(&:present?)

or

next if [product, version].any?(&:nil?)
Mori
  • 27,279
  • 10
  • 68
  • 73