Reputation: 27224
I'm having a bit of trouble converting the following method in a Google App Script to work with Rails:
Tutorial: Parsing an XML Document
This is the method I'd like to convert:
function parse(txt) {
var doc = Xml.parse(txt, true);
var attr = doc.spellcheck;
if (attr) {
return "Cannot find actor: " + attr.name;
}
var actors = doc.html.head.getElements("actor");
var movies = doc.html.head.getElements("movie");
if (!actors || actors.length ==0) {
return "no match found";
}
var movieIndex = 0;
var r = '';
var firstPerson = true;
for (var i in actors) {
r = r + actors[i].getText();
if (movies[movieIndex]) {
r = r + (firstPerson ? "" : " who") + " was in " +
movies[movieIndex].getText() + " with ";
}
movieIndex++;
firstPerson = false;
}
return r;
}
This is what I have so far:
I'm using Nokogiri
to parse the XML:
uri = "http://oracleofbacon.org/cgi-bin/xml?a=Kevin%20Bacon&b=#{to.strip.to_hash}&u=1&p=google-apps"
doc = Nokogiri::XML(open(uri))
My attempt at conversion:
def parse(doc)
attr = doc.xpath("//link//actor").first # => "<actor>Miley Cyrus</actor>"
if (!attr)
return "Cannot find actor: #{attr}"
end
actors = doc.xpath("//link//actor")
movies = doc.xpath("//link//movie")
if (!actors || actors.length == 0)
return "No match found."
end
movieIndex = 0
r = ""
firstPerson = true
actors.each do |actor|
r = r + actor
if (movies[movieIndex])
r = r + (firstPerson ? "" : " who") + " was in #{movies[movieIndex]} with "
end
movieIndex++
firstPerson = false;
end
return r
end
I get an error:
`block in parse': undefined method `+@' for false:FalseClass (NoMethodError)
This is happening on the firstPerson = false
line.
Upvotes: 1
Views: 385
Reputation: 20125
actors.each
yields each actor node to the block. So your i
variable contains an actor element (Nokogiri::XML::Element
). Thus actors[i]
doesn't make sense, which is why you get the first error.
You're probably looking for
actors.each do |actor|
r = r + actor
[...]
end
although I am not sure what will happen when you attempt to concatenate a Nokogiri::XML::Element
to a String
.
Also, movieIndex++
doesn't do what you hope. You want movieIndex += 1
.
Although, since you're just incrementing movieIndex
on every loop, you could remove the line entirely and do actors.each do |actor, movieIndex|
instead.
Upvotes: 4