Donavan White
Donavan White

Reputation: 1126

Create multiple nodes inside Nokogiri::XML::Builder code block using a loop

I have code that needs to loop and it will need to change the node name through the each loop, here is an example of what I mean:

builder = Nokogiri::XML::Builder.new do |xml|
  xml.HasThreeNodes {
    ['Node1','Node2','Node3'].each do |name|
      xml.name "value"
    end
  }
end

Output:

<?xml version="1.0"?>
<HasThreeNodes>
  <name>value</name>
  <name>value</name>
  <name>value</name>
</HasThreeNodes>

I have tried different variations of code, but to no avail. I just need where it says <name> to be <Node1> and so on. Any help is much appreciated, thanks.

Upvotes: 1

Views: 687

Answers (1)

pguardiario
pguardiario

Reputation: 55002

This works:

xml.send name, "value"

Also why mix curlies and do/ends? It looks better if it's consistent.

Upvotes: 2

Related Questions