Skip to content

using XML::Builder to build a doc node #3065

Answered by flavorjones
stoivo asked this question in General Q&A
Discussion options

You must be logged in to vote

Hi! Thanks for asking this question. Because it falls under user help, I've converted it to a discussion (hope that's OK).

The builder allows you to create nodes for names bound to pre-existing methods by appending an underscore (_). Docs explaining this are here.

Here's an example of what happens when you use doc versus doc_:

#!/usr/bin/env ruby

require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "nokogiri"
end

builder = Nokogiri::XML::Builder.new do |xml|
  xml.root {
    xml.doc {
      xml.thing "content"
    }
  }
end
builder.to_xml
# => "<?xml version=\"1.0\"?>\n" + "<root/>\n"

builder = Nokogiri::XML::Builder.new do |xml|
  xml.root {
    xml.doc_ {
      xml

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@stoivo
Comment options

@flavorjones
Comment options

Answer selected by stoivo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state/needs-triage Inbox for non-installation-related bug reports or help requests
2 participants
Converted from issue

This discussion was converted from issue #3064 on December 18, 2023 22:30.